Files
react-native/packages
Alan LeeandReact Native Bot 0e3764c8aa migrate deprecated onBackPressed() (#51096)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
2025-05-19 15:32:24 +00:00
..
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 17:41:07 +00:00
2025-05-12 16:16:21 +00:00
2025-05-12 16:16:21 +00:00