-
Notifications
You must be signed in to change notification settings - Fork 25k
Android: fix ClassCastException in ReactRootView.java when software keyboard is shown #40755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android: fix ClassCastException in ReactRootView.java when software keyboard is shown #40755
Conversation
|
Hi @kot331107! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Base commit: f00594b |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
What happens if you try to get the activity from the context of the result of "getRootView()", instead of the React root view? |
It gives me same exception but with different type Well, while debugging in ReactRootView I found that it hits twice in the getActivity() in my reference app where I follow repro steps. The first time it hits for the |
|
In this scenario where we are called into twice, do we have multiple root views visible at once subscribing to these events? If it's possible to get into this case, where we cannot resolve an activity to resolve the current window, we could ideally find a reliable and not to invasive way to get the current window softinputmode. Searching around, I think we might be able to get this information off of the root DecorView in the hierarchy as WindowManager.LayoutParams, without needing to resolve an activity which created the view. https://stackoverflow.com/a/61768230 Could we maybe test that approach for resolving softInputMode? |
|
@NickGerleman I haven't found a way to get it from the root DecorView but I'm able to handle my case by iterating |
|
oh wait, I think I found something more robust :D |
|
@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
Just to check, the snippet in the above linked post didn't work for your scenario? private WindowManager.LayoutParams tryGetWindowParams()
{
View view = this;
if (view.getLayoutParams() instanceof WindowManager.LayoutParams)
return (WindowManager.LayoutParams) view.getLayoutParams();
while (view.getParent() instanceof View)
{
view = (View) view.getParent();
if (view.getLayoutParams() instanceof WindowManager.LayoutParams)
return (WindowManager.LayoutParams) view.getLayoutParams();
}
return null;
} |
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
Outdated
Show resolved
Hide resolved
hey thanks for double checking! I got what you suggested. This works as well in my case. Thank you for your help @NickGerleman and please see the latest changes UPD I merged with the latest from main and the test_android_template-... builds are failing now. I don't believe it is related to my changes 🤷♂️ |
| int softInputMode = getActivity().getWindow().getAttributes().softInputMode; | ||
| int softInputMode; | ||
| Activity activity = getActivity(); | ||
| if (activity != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need the activity, right?
|
Yeah, Android template issue seems unrelated. Thanks for the work you’re doing here, I think after we remove the no longer needed code for getting an activity, we should merge this. |
|
@NickGerleman my pleasure doing something useful :D Ah I see, they used https://github.com/stripe/stripe-react-native for testing. I'm gonna try to setup and test with it as well. |
|
In my test app it works well after removing getActivity(). Trying to test out with stripes app as well |
|
If local debugging shows you are able to get the root window params with this, and tests pass, I think we are probably fine. Ideally we'd have some better test coverage around keyboard events (we have seen issues there before, esp for older Android versions). |
yeah I checked with debugger, sure thing |
|
it seems I should fix tests as well |
|
TIL we have tests for this 😅. It looks like |
|
I wasn't able to setup tests in react-native quickly (gradle doesn't completely sync the project) so making a blind fix for tests :/ |
I'd say do the following:
|
|
@cortinico I think I tried doing it from terminal too. Anyways test_android was fixed and the remaining failing checks shouldn't be related to this pr cc @NickGerleman |
|
Lemme try to merge it with the latest from main |
|
/rebase |
…react/ReactRootView.java add Nullable annotation to getActivity() Co-authored-by: Nick Gerleman <[email protected]>
2e04640 to
693957c
Compare
|
|
|
@cortinico btw, I resolved my local issues with building react-native by pinning the JDK version in gradle.properties. I have several JDK installed on my comp and that was the culprit. |
|
@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
This pull request was successfully merged by @kot331107 in 9497203. When will my fix make it into a release? | Upcoming Releases |
|
@kot331107 would you be able to give #40970 a try? |
…eyboard is shown (facebook#40755) Summary: Fixes facebook#40754 Hi all! We noticed that our app started to crash after bumping to RN v0.71.13, anyways after a deeper investigation we also found that the crash occurs in the latest version as well. Crash log: ``` E FATAL EXCEPTION: main Process: com.nfl.fantasy.core.android.debug, PID: 6034 java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.getActivity(ReactRootView.java:926) at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.checkForKeyboardEvents(ReactRootView.java:946) at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.onGlobalLayout(ReactRootView.java:912) at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1061) ``` The code which causes ClassCastException is following [here](https://github.com/facebook/react-native/blob/ea88fbe229e1d276753ee8e118184274fc872138/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L864). In this code explicit type conversion to Activity is not safe because it's not guaranteed by the compiler that context will be compatible with Activity type. The appropriate issue [has been filed](facebook#40754). <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [ANDROID] [FIXED] - Fixed crash occurring in certain native views when keyboard events are fired. Pull Request resolved: facebook#40755 Test Plan: Tested it manually with the [reference application](https://github.com/kot331107/rnCrashReproducer). Repro steps are as follows: - Build and run the app on Android - Tap the button "Open Modal" - You should see the red popup fragment to the bottom of the screen - Tap on the text input to open software keyboard - Expected: it should show the keyboard and no crash happens. Reviewed By: arushikesarwani94 Differential Revision: D50198424 Pulled By: NickGerleman fbshipit-source-id: a5a6d86334856f4ffbe818150da5793380da4702
* Fix android platform border color (#39893) Summary: If you try to apply PlatformColor to borders on Android app will crash with the next error: "Error while updating property 'borderColor' of a view managed by: RCTView" ## Changelog: [ANDROID] [FIXED] - Fix android crash when apply PlatformColor to borders Pull Request resolved: #39893 Test Plan: In RNTester example, go to APIs -> PlatformColor | Before | After | | ----------- | ----------- | | <img src="https://github.com/facebook/react-native/assets/70860930/66ac2880-53da-4438-bd9a-332f8ea40645" alt="drawing" width="200"/> | <img src="https://github.com/facebook/react-native/assets/70860930/151f58a1-d857-4b3d-9ec6-de74eb065127" alt="drawing" width="200"/> | Reviewed By: NickGerleman Differential Revision: D50011758 Pulled By: javache fbshipit-source-id: ea06c18c6aef4b6731e9b9b87422a1e0d13de208 * Android: fix ClassCastException in ReactRootView.java when software keyboard is shown (#40755) Summary: Fixes #40754 Hi all! We noticed that our app started to crash after bumping to RN v0.71.13, anyways after a deeper investigation we also found that the crash occurs in the latest version as well. Crash log: ``` E FATAL EXCEPTION: main Process: com.nfl.fantasy.core.android.debug, PID: 6034 java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.getActivity(ReactRootView.java:926) at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.checkForKeyboardEvents(ReactRootView.java:946) at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.onGlobalLayout(ReactRootView.java:912) at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1061) ``` The code which causes ClassCastException is following [here](https://github.com/facebook/react-native/blob/ea88fbe229e1d276753ee8e118184274fc872138/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L864). In this code explicit type conversion to Activity is not safe because it's not guaranteed by the compiler that context will be compatible with Activity type. The appropriate issue [has been filed](#40754). <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [ANDROID] [FIXED] - Fixed crash occurring in certain native views when keyboard events are fired. Pull Request resolved: #40755 Test Plan: Tested it manually with the [reference application](https://github.com/kot331107/rnCrashReproducer). Repro steps are as follows: - Build and run the app on Android - Tap the button "Open Modal" - You should see the red popup fragment to the bottom of the screen - Tap on the text input to open software keyboard - Expected: it should show the keyboard and no crash happens. Reviewed By: arushikesarwani94 Differential Revision: D50198424 Pulled By: NickGerleman fbshipit-source-id: a5a6d86334856f4ffbe818150da5793380da4702 * chore: bump podfile.lock --------- Co-authored-by: Ivan Alexandrov <[email protected]> Co-authored-by: Filipp Mikheev <[email protected]>
Summary:
Fixes #40754
Hi all!
We noticed that our app started to crash after bumping to RN v0.71.13, anyways after a deeper investigation we also found that the crash occurs in the latest version as well.
Crash log:
The code which causes ClassCastException is following here.
In this code explicit type conversion to Activity is not safe because it's not guaranteed by the compiler that context will be compatible with Activity type.
The appropriate issue has been filed.
Changelog:
[ANDROID] [FIXED] - Fixed crash occurring in certain native views when keyboard events are fired.
Test Plan:
Tested it manually with the reference application. Repro steps are as follows: