Skip to content
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] Add onUserLeaveHint support to ReactActivityDelegate #42741

Closed

Conversation

behenate
Copy link
Contributor

Summary:

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.

@facebook-github-bot
Copy link
Contributor

Hi @behenate!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@analysis-bot
Copy link

analysis-bot commented Jan 30, 2024

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 17,066,849 -114,283
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 20,450,443 -109,482
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: ae42e02
Branch: main

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Jan 31, 2024
@cortinico cortinico added Partner p: Software Mansion Partner: Software Mansion labels Jan 31, 2024
@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Feb 1, 2024
@facebook-github-bot
Copy link
Contributor

@cortinico merged this pull request in 3b6c522.

@cortinico
Copy link
Contributor

This needs to be reverted as it's introducing a crash internally. I'll follow up on it tomorrow @behenate

@facebook-github-bot
Copy link
Contributor

This pull request has been reverted by 78f5002.

Comment on lines +601 to +609
if (mCurrentActivity != null) {
Assertions.assertCondition(
activity == mCurrentActivity,
"Called onUserLeaveHint on an activity that is not the current activity, this is incorrect! "
+ "Current activity: "
+ mCurrentActivity.getClass().getSimpleName()
+ " "
+ "Leaving activity: "
+ activity.getClass().getSimpleName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behenate Ok so one of our product was crashing on this assert
I haven't had the time to investigate what's going on, but practically we should understand if this assert is actually necessary.

That's the stacktrace:

02-01 14:38:52.320  4500  4522 V fb-android-monitor: java.lang.AssertionError
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.facebook.infer.annotation.Assertions.assertCondition(Assertions.java:82)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.facebook.react.ReactInstanceManager.onUserLeaveHint(ReactInstanceManager.java:598)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.facebook.react.ReactDelegate.onUserLeaveHint(ReactDelegate.java:107)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.facebook.react.ReactActivityDelegate.onUserLeaveHint(ReactActivityDelegate.java:127)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.facebook.react.ReactActivity.onUserLeaveHint(ReactActivity.java:111)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.Activity.performUserLeaving(Activity.java:8189)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.Instrumentation.callActivityOnUserLeaving(Instrumentation.java:1520)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.ActivityThread.performUserLeavingActivity(ActivityThread.java:4659)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4643)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.os.Handler.dispatchMessage(Handler.java:106)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.os.Looper.loop(Looper.java:223)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at android.app.ActivityThread.main(ActivityThread.java:7656)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at java.lang.reflect.Method.invoke(Native Method)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
02-01 14:38:52.320  4500  4522 V fb-android-monitor: 	at Z.init(unknown)

Could you resubmit 3b6c522 without this assert?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behenate are you able to follow-up here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cortinico Sorry for no reply, I'm currently out of office until February 14th, I will follow up when I'm back :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure no rush 👍

facebook-github-bot pushed a commit that referenced this pull request Mar 15, 2024
Summary:
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](https://developer.android.com/develop/ui/views/picture-in-picture#:~:text=You%20might%20want%20to%20include%20logic%20that%20switches%20an%20activity%20into%20PiP%20mode%20instead%20of%20going%20into%20the%20background.%20For%20example%2C%20Google%20Maps%20switches%20to%20PiP%20mode%20if%20the%20user%20presses%20the%20home%20or%20recents%20button%20while%20the%20app%20is%20navigating.%20You%20can%20catch%20this%20case%20by%20overriding%20onUserLeaveHint()%3A) for android < 12.

This is a re-submission of #42741. The problematic `assert` has been changed to an `if` - it's not a problem if onUserLeaveHint is received from an activity different to the current one, but in that case we shouldn't emit the event.

## Changelog:

[ANDROID] [ADDED] - Added `onUserLeaveHint` support into `ReactActivityDelegate`

Pull Request resolved: #43488

Test Plan: Tested in the `rn-tester` app - callbacks are correctly called on both old and new architecture.

Reviewed By: javache

Differential Revision: D54905564

Pulled By: cortinico

fbshipit-source-id: 3f24271405f66bf3a9450320a484e522224eefc1
facebook-github-bot pushed a commit that referenced this pull request Mar 20, 2024
Summary:
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](https://developer.android.com/develop/ui/views/picture-in-picture#:~:text=You%20might%20want%20to%20include%20logic%20that%20switches%20an%20activity%20into%20PiP%20mode%20instead%20of%20going%20into%20the%20background.%20For%20example%2C%20Google%20Maps%20switches%20to%20PiP%20mode%20if%20the%20user%20presses%20the%20home%20or%20recents%20button%20while%20the%20app%20is%20navigating.%20You%20can%20catch%20this%20case%20by%20overriding%20onUserLeaveHint()%3A) for android < 12.

This is a re-submission of #42741 and #43488 without problematic asserts, which were unnecessary (`onUserLeaveHint` is not critical to the lifecycle of the app), but were causing problems in some apps.

## Changelog:

[ANDROID] [ADDED] - Added `onUserLeaveHint` support into `ReactActivityDelegate`

Pull Request resolved: #43567

Test Plan: Tested in the `rn-tester` app - callbacks are correctly called on both old and new architecture.

Reviewed By: javache

Differential Revision: D55123632

Pulled By: cortinico

fbshipit-source-id: 144a1d84b691af9cf3c0cffad446e674b4b68927
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Software Mansion Partner: Software Mansion Partner Reverted Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants