Skip to content

Grzegorzkruk/predictive back swipe - #59292

Draft
grzegorzkruk wants to merge 13 commits into
react:mainfrom
grzegorzkruk:grzegorzkruk/predictive-back-swipe
Draft

grzegorzkruk wants to merge 13 commits into
react:mainfrom
grzegorzkruk:grzegorzkruk/predictive-back-swipe

Conversation

@grzegorzkruk

@grzegorzkruk grzegorzkruk commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary:

On Android 16+ with targetSdk 36, Android turns on predictive back by default and no longer calls Activity.onBackPressed(). To keep BackHandler working, #51096 added an OnBackPressedCallback to ReactActivity that is always enabled.

An enabled callback tells Android "this app handles back itself". So Android never plays its predictive back animation (back to home, back to the previous activity), and FragmentManager never receives the swipe. Every React Native app targeting SDK 36 is quietly opted out of predictive back, even apps that never use BackHandler. Native navigation libraries (react-native-screens) and brownfield hosts have to fight this callback, often with reflection.

Related RCP

This PR implements the first part of the RCP "Predictive back gesture support in React Native applications".

The RCP proposes that React Native work with Android's back model instead of against it. React Native takes back only when JavaScript says, ahead of time, that it has something to go back to; otherwise Android handles back and plays its animation. BackHandler keeps its API but has smaller guarantees on Android: it's off by default, and native code can override it.

RCP point In this PR
React Native's back callback is off by default (targetSdk 36+) ✅
BackHandler.setInterceptEnabled(enabled), a preference and not a guarantee ✅
ReactActivity.getBackPressedCallback() for native libraries and brownfield hosts ✅
Don't re-enable the callback after the default back action ✅
Separate "back happened" event using PRIORITY_SYSTEM_NAVIGATION_OBSERVER ⏸️ Follow-up, still open in the RCP
setInterceptEnabled reaching the calling surface's activity (brownfield) ⏸️ Follow-up, still open in the RCP
Gesture progress and predictive back animations ❌ Not in core; left to community libraries such as react-native-screens

The RCP is based on research in a brownfield demo app built on react-native-brownfield (native screen → React Native screen → native screen with an embedded React Native fragment). The research also looked at an observer-only alternative and rejected it as a replacement for BackHandler: an observer can't block back, works only on Android 16+, allows one per window on Android 16, and fires after the activity has already closed.

What this PR changes

  • The callback is registered disabled. With no callback enabled, Android handles back and plays its animation. Apps get predictive back without any setup.
  • New BackHandler.setInterceptEnabled(enabled) (Android; a no-op on iOS). JavaScript turns it on while it has something to go back to, for example a pushed JS screen or an open modal. While it's on, hardwareBackPress listeners run as before, including return true to block back, and the system animation doesn't play. While it's off, Android handles back. It goes through a new setInterceptEnabled method on the existing DeviceEventManager TurboModule spec. This is a preference, not a guarantee: a callback that native code enables later still wins.
  • New ReactActivity.getBackPressedCallback() for native libraries and brownfield hosts that need to own back, so they no longer need reflection.
  • invokeDefaultOnBackPressed() restores the previous enabled state instead of always re-enabling the callback. Otherwise BackHandler.exitApp() would turn interception on, and re-enable a callback that a navigation library had disabled.
  • RNTester turns interception on only while a module is open. The root list gets the system animation when leaving the app.

Below targetSdk 36 nothing changes: the callback is never added to the dispatcher, and hardwareBackPress is delivered as before.

Behaviour change. On targetSdk 36, apps that rely on hardwareBackPress must opt in:

useEffect(() => {
  BackHandler.setInterceptEnabled(canGoBack);
  const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
    goBack();
    return true;
  });
  return () => {
    subscription.remove();
    BackHandler.setInterceptEnabled(false);
  };
}, [canGoBack]);

**Known limitation.** `setInterceptEnabled` acts on `currentActivity`. In brownfield apps, the activity in front is sometimes a native one, and then the call is ignored. Targeting the calling surface's activity is an open question in the RCP.

**Open questions for review** (also tracked in the RCP):
1. Should the new default ship behind a `ReactNativeFeatureFlags` entry for one release, so apps have time to migrate?
2. Naming: `setInterceptEnabled` describes the mechanism. Alternatives: `setBackHandlerEnabled`, `requestBackHandling`.
3. Should there be a dev-mode warning when a `hardwareBackPress` listener exists while interception is off?

## Changelog:

[ANDROID] [BREAKING] - On targetSdk 36, React Native no longer blocks the predictive back animation by default. Call the new `BackHandler.setInterceptEnabled(true)` to receive `hardwareBackPress`; native libraries can use `ReactActivity.getBackPressedCallback()`.

## Test Plan:

Automated:

yarn build-types # regenerates ReactNativeApi.d.ts
./gradlew :packages:react-native:ReactAndroid:compileDebugKotlin
./gradlew :packages:react-native:ReactAndroid:compileDebugJavaWithJavac
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest
--tests "com.facebook.react.ReactActivityBackInterceptionTest"
./gradlew :packages:react-native:ReactAndroid:ktfmtCheck
npx eslint --max-warnings 0
npx prettier --check
yarn test packages/react-native/Libraries/LogBox/tests # BackHandler mock consumers


All passed: 6/6 in `ReactActivityBackInterceptionTest`, 39/39 in LogBox. The tests cover:
- the callback is disabled by default;
- `setInterceptEnabled` turns it on and off;
- `invokeDefaultOnBackPressed` neither enables interception that was never requested, nor turns off interception that was;
- `invokeDefaultOnBackPressed` still finishes the activity.

`ReactAndroid.api` was updated by hand, because `apiDump` is disabled in my checkout. CI should confirm the snapshot.

Manual, on RNTester (Android 16+ emulator, gesture navigation, `targetSdk` 36):
1. On the root list, swipe from the left edge. Expected: the system predictive back animation plays and the app exits.
2. Open a module, then press or swipe back. Expected: it returns to the list.
3. From a module, call `BackHandler.exitApp()`. Expected: the app exits, and interception isn't left on afterwards.

@meta-cla

meta-cla Bot commented Oct 21, 2026

Copy link
Copy Markdown

Hi @grzegorzkruk!

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!

@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

…n ReactActivity

Summary:
- Added new methods to `ReactActivity` to improve back navigation handling on Android API 36 and above, including `getBackPressedCallback()` and `onSystemNavigationBackInvoked()`.
- Introduced a `SystemNavigationBackObserver` to manage system back navigation events.
- Added unit tests for the new back navigation functionality in `ReactActivityBackPressedCallbackTest.kt`.
- Updated `RNTesterActivity` to allow system predictive-back animations while still notifying JS on app exit.

Changelog:
[GENERAL][CHANGED] - Enhanced back navigation handling and updated hermes-compiler version.

Test Plan:
- Verified back navigation functionality on Android API 36+.
- Ensured that the updated hermes-compiler works without issues in the project.
…ation handling

Summary:
- Introduced `setInterceptEnabled(enabled: boolean)` method to `BackHandler` for Android, allowing React Native to manage back gesture interception.
- Updated `ReactActivity` to handle predictive back animations on Android 16+.
- Enhanced type definitions in TypeScript and Flow to include the new method.
- Added unit tests to verify the functionality of the new back navigation feature.

Changelog:
[GENERAL][CHANGED] - Enhanced back navigation handling with new interception capabilities.
Summary:
- Introduced `PredictiveBackAnimatedView` and related types for handling predictive back gestures in React Native.
- Enhanced `BackHandler` to support predictive back events, allowing developers to listen for gesture phases and progress.
- Updated `ReactActivity` to manage predictive back handlers and events, improving back navigation experience on Android.
- Added unit tests to validate the new predictive back functionality and ensure proper event handling.

Changelog:
[GENERAL][ADDED] - Support for predictive back navigation in BackHandler and ReactActivity.
Summary:
- Introduced `PredictiveBackAnimatedView` for handling predictive back gestures on Android.
- Added `usePredictiveBackAnimatedValue` hook to facilitate animated responses to back gesture progress.
- Updated type definitions and flow types to include new predictive back event types and properties.
- Enhanced `ReactActivity` to manage predictive back progress listeners without consuming the gesture.
- Added unit tests to validate the new predictive back functionality.

Changelog:
[GENERAL][ADDED] - Support for predictive back navigation with new components and hooks.
Summary:
- Updated `BackHandler` and `ReactActivity` to improve predictive back gesture handling on Android.
- Removed deprecated `PREDICTIVE_BACK_FRONT_PANE_NATIVE_ID` and adjusted related logic to streamline predictive back animations.
- Enhanced type definitions to reflect changes in predictive back event handling.
- Updated `RNTesterApp` and Playground example to demonstrate new predictive back functionality without the deprecated ID.
- Cleaned up unit tests to align with the new implementation.

Changelog:
[GENERAL][CHANGED] - Improved predictive back navigation handling and removed deprecated identifiers.
…functionality

Summary:
- Removed `usePredictiveBackAnimatedValue` hook and related predictive back event types from `BackHandler` and `ReactActivity`.
- Updated `PredictiveBackAnimatedView` to streamline predictive back gesture handling.
- Adjusted type definitions and flow types to reflect the removal of deprecated predictive back features.
- Cleaned up related tests and examples to align with the new implementation.

Changelog:
[GENERAL][REMOVED] - Deprecated predictive back functionality and related types.
Summary:
- Introduced `claimPredictiveBack` method to `BackHandler` for managing back gesture ownership in overlays.
- Updated `ReactActivity` to support predictive back claims, allowing for better control over back gesture handling.
- Enhanced type definitions to include new methods and properties related to predictive back functionality.
- Added unit tests to validate the new predictive back behavior and ensure proper event handling.

Changelog:
[GENERAL][ADDED] - Support for predictive back gesture management in BackHandler and ReactActivity.
Summary:
- Removed `claimPredictiveBack` method from `BackHandler` and related files to streamline back gesture management.
- Updated type definitions and interfaces to reflect the removal of predictive back claims.
- Cleaned up associated tests and mocks to ensure consistency with the new implementation.

Changelog:
[GENERAL][REMOVED] - Deprecated predictive back claim functionality from BackHandler and related components.
Summary:
- Introduced a RCP file detailing the predictive back gesture support in React Native applications.

Changelog:
[GENERAL][ADDED] - RCP for predictive back gesture support in React Native.
@grzegorzkruk
grzegorzkruk force-pushed the grzegorzkruk/predictive-back-swipe branch from 2edf75f to 651de61 Compare October 22, 2026 10:24
@meta-cla

meta-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla 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 Oct 22, 2026

This branch has not been deployed

No deployments
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants