Skip to content

migrate deprecated onBackPressed() - #51096

Closed
alanleedev wants to merge 1 commit into
react:mainfrom
alanleedev:export-D74161428
Closed

alanleedev wants to merge 1 commit into
react:mainfrom
alanleedev:export-D74161428

Conversation

@alanleedev

Copy link
Copy Markdown
Contributor

Summary:
Problem: Activity.onBackPressed() has been deprecated and with targetSdk 36, predictive back will be enforced and the API no longer called. We need to migrate to backward compatible OnBackPressedCallback.

Solution:
OnBackPressedCallback is registered conditionally only if it is targetSdk 36 or greater.
If the callback in enabled, onBackPressed() is not called and callback is used regardless of android:enableOnBackInvokedCallback property in or set in AndroidManifest.xml.

As a workaround callback is manually calling existing onBackPressed().

This is done rather than removing onBackPressed() completely and using only OnBackPressedCallback as we are not sure of the impact of removing the implementation entirely. Once we determine it is safe to do so then, we should remove the workaround and fully transition to OnBackPressedCallback

NOTE: ReactDelegate.onHostResume() sets up the DefaultHardwareBackBtnHandler using ReactActivity (https://fburl.com/ul47tbeo) and will be called from JS BackHanderl.exitApp (https://fburl.com/code/a4l2pjsw).

Changelog:
[Internal]

Differential Revision: D74161428

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 5, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D74161428

Summary:

**Problem:** `Activity.onBackPressed()` has been deprecated and with targetSdk 36, predictive back will be enforced and the API no longer called. We need to migrate to backward compatible AndroidX `OnBackPressedCallback`.
- https://developer.android.com/about/versions/16/behavior-changes-16#predictive-back.

**Solution:**
`OnBackPressedCallback` is registered conditionally only if it is `targetSdk` 36 or greater.
If the callback in enabled, `onBackPressed()` is not called and callback is used regardless of `android:enableOnBackInvokedCallback` property in <application> or <activity> set in AndroidManifest.xml.

As a workaround callback is manually calling existing `onBackPressed()`.

This is done rather than removing onBackPressed() completely and using only `OnBackPressedCallback` as we are not sure of the impact of removing the implementation entirely. Once we determine it is safe to do so then, we should remove the workaround and fully transition to `OnBackPressedCallback`

* I also surveyed child classes extending ReactActivity for overridden `onPressedBack()` usage and found only one usage which will be handled later.

NOTE: `ReactDelegate.onHostResume()` sets up the `DefaultHardwareBackBtnHandler` using `ReactActivity` (https://fburl.com/ul47tbeo) and will be called from JS `BackHanderl.exitApp` (https://fburl.com/code/a4l2pjsw). Calling `BackHanderl.exitApp` enables predictive back to work.


Changelog:
[Internal]

Reviewed By: mdvacca

Differential Revision: D74161428
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D74161428

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label May 8, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request has been merged in bc876fb.

@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @alanleedev in bc876fb

When will my fix make it into a release? | How to file a pick request?

react-native-bot pushed a commit that referenced this pull request May 19, 2025
Summary:
Pull Request resolved: #51096

**Problem:** `Activity.onBackPressed()` has been deprecated and with targetSdk 36, predictive back will be enforced and the API no longer called. We need to migrate to backward compatible AndroidX `OnBackPressedCallback`.
- https://developer.android.com/about/versions/16/behavior-changes-16#predictive-back.

**Solution:**
`OnBackPressedCallback` is registered conditionally only if it is `targetSdk` 36 or greater.
If the callback in enabled, `onBackPressed()` is not called and callback is used regardless of `android:enableOnBackInvokedCallback` property in <application> or <activity> set in AndroidManifest.xml.

As a workaround callback is manually calling existing `onBackPressed()`.

This is done rather than removing onBackPressed() completely and using only `OnBackPressedCallback` as we are not sure of the impact of removing the implementation entirely. Once we determine it is safe to do so then, we should remove the workaround and fully transition to `OnBackPressedCallback`

* I also surveyed child classes extending ReactActivity for overridden `onPressedBack()` usage and found only one usage which will be handled later.

NOTE: `ReactDelegate.onHostResume()` sets up the `DefaultHardwareBackBtnHandler` using `ReactActivity` (https://fburl.com/ul47tbeo) and will be called from JS `BackHanderl.exitApp` (https://fburl.com/code/a4l2pjsw). Calling `BackHanderl.exitApp` enables predictive back to work.

Changelog:
[Internal]

Reviewed By: mdvacca

Differential Revision: D74161428

fbshipit-source-id: 2e081ba6922b315e9d1746e83a41bab5277fa62e
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @alanleedev in 0e3764c

When will my fix make it into a release? | How to file a pick request?

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. fb-exported Merged This PR has been merged. p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants