[Android] Add onUserLeaveHint support to ReactActivityDelegate
Summary:
This is a resubmission of the previous PRs. We are trying to figure out why it causes crashes in some apps.
This PR adds onUserLeaveHint support into the ReactActivityDelegate. It allows modules to receive an event every time user moves the app into the background. This is slightly different than onPause - it's called only when the user intentionally moves the app into the background, e.g. when receiving a call onPause should be called but onUserLeaveHint shouldn't.
This feature is especially useful for libraries implementing features like Picture in Picture (PiP), where using onUserLeaveHint is the recommended way of auto-entering PiP for android < 12.
Changelog:
[ANDROID] [ADDED] - Added onUserLeaveHint support into ReactActivityDelegate
Test Plan:
Tested in the rn-tester app - callbacks are correctly called on both old and new architecture.
| Platform | Engine | Arch | Size (bytes) | Diff |
|---|---|---|---|---|
| android | hermes | arm64-v8a | 18,073,881 | -376,780 |
| android | hermes | armeabi-v7a | n/a | -- |
| android | hermes | x86 | n/a | -- |
| android | hermes | x86_64 | n/a | -- |
| android | jsc | arm64-v8a | 21,440,617 | -375,017 |
| android | jsc | armeabi-v7a | n/a | -- |
| android | jsc | x86 | n/a | -- |
| android | jsc | x86_64 | n/a | -- |
Base commit: 6d0f72bf0bd30b571019ed6e8cf47bc2ff97be79 Branch: main
@cortinico Any updates on the source of the crashes?
@cortinico Any updates on the source of the crashes?
I haven't had the time to test again sorry. Hopefully I should have an answer by next week
@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.
@behenate do you want to get back to this? (sorry for the late reply). If so, can I ask you to rebase?
@cortinico Sure! Let's try to find what's been causing the crashes last time
@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.
I did some investigation and there was a NPE here:
07-02 17:17:09.277 7756 7756 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.react.ReactDelegate.onUserLeaveHint()' on a null object reference
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at com.facebook.react.ReactActivityDelegate.onUserLeaveHint(ReactActivityDelegate.java:142)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at com.facebook.react.ReactActivity.onUserLeaveHint(ReactActivity.java:119)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.Activity.performUserLeaving(Activity.java:9182)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.Instrumentation.callActivityOnUserLeaving(Instrumentation.java:1786)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.ActivityThread.performUserLeavingActivity(ActivityThread.java:5458)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:5438)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:48)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:60)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeLifecycleItem(TransactionExecutor.java:282)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:150)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:93)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2595)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:232)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.os.Looper.loop(Looper.java:317)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8592)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
07-02 17:17:09.277 7756 7756 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
I've fixed it with a null check and will attempt another land. Will keep you posted if it causes further crashes.
@cortinico merged this pull request in facebook/react-native@27ba369f2325db5678aaf6a18927c127921840e0.
This pull request was successfully merged by @behenate in 27ba369f2325db5678aaf6a18927c127921840e0.
When will my fix make it into a release? | How to file a pick request?