diff --git a/packages/react-native/Libraries/Utilities/BackHandler.android.js b/packages/react-native/Libraries/Utilities/BackHandler.android.js index b469ff2a955d..5c6e32daa62e 100644 --- a/packages/react-native/Libraries/Utilities/BackHandler.android.js +++ b/packages/react-native/Libraries/Utilities/BackHandler.android.js @@ -9,6 +9,7 @@ */ import NativeDeviceEventManager from '../../Libraries/NativeModules/specs/NativeDeviceEventManager'; +import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags'; import {setEventInitTimeStamp} from '../../src/private/webapis/dom/events/internals/EventInternals'; import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter'; import {HardwareBackPressEvent} from './HardwareBackPressEvent'; @@ -18,7 +19,37 @@ const DEVICE_BACK_EVENT = 'hardwareBackPress'; type BackPressEventName = 'backPress' | 'hardwareBackPress'; type BackPressHandler = (event: HardwareBackPressEvent) => ?boolean | void; -const _backPressSubscriptions: Array = []; +type BackPressSubscription = { + handler: BackPressHandler, + intercept: boolean, +}; + +const _backPressSubscriptions: Array = []; + +/** + * Turns interception on while at least one listener asks for it, and off + * when none does. Android reads this before the back gesture starts, so it + * has to be up to date ahead of time. + * + * With the `enablePredictiveBackAndroid` feature flag on, it sends the value on + * every change to the listeners, even when it is the same as before, so a + * repeated request (for example when a screen gets focus) applies again if + * native code or another activity changed it. With the flag off and no + * listener asking for back, nothing is sent. + */ +function updateIntercept(): void { + const interceptEnabled = _backPressSubscriptions.some( + subscription => subscription.intercept, + ); + if ( + !interceptEnabled && + !ReactNativeFeatureFlags.enablePredictiveBackAndroid() + ) { + return; + } + // Optional: native code built before this method existed doesn't have it. + NativeDeviceEventManager?.setInterceptEnabled?.(interceptEnabled); +} RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function (nativeEvent) { const options = {}; @@ -28,7 +59,7 @@ RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function (nativeEvent) { } const event = new HardwareBackPressEvent(options); for (let i = _backPressSubscriptions.length - 1; i >= 0; i--) { - if (_backPressSubscriptions[i]?.(event)) { + if (_backPressSubscriptions[i]?.handler(event)) { return; } } @@ -36,6 +67,29 @@ RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function (nativeEvent) { BackHandler.exitApp(); }); +type BackHandlerOptions = { + /** + * Android 16+ with targetSdk 36+ only. Ask Android to send back to React + * Native while this listener exists, instead of handling back itself with + * the predictive back animation. React Native intercepts back while at + * least one listener asks for it. Set it ahead of time, for example to your + * navigator's `canGoBack`. Defaults to `false`. + * + * It is a request, not a guarantee: native code that adds its own back + * callback after React Native's still receives back first. + */ + intercept?: boolean, +}; + +type BackHandlerSubscription = { + remove: () => void, + /** + * Changes this listener's `intercept` request without removing it. + */ + setInterceptEnabled: (enabled: boolean) => void, + ... +}; + /** * Detects hardware button presses for back navigation and lets you register * event listeners for the system's back action. Event subscriptions are called @@ -50,7 +104,8 @@ type TBackHandler = { readonly addEventListener: ( eventName: BackPressEventName, handler: BackPressHandler, - ) => {remove: () => void, ...}, + options?: BackHandlerOptions, + ) => BackHandlerSubscription, }; const BackHandler: TBackHandler = { /** @@ -67,21 +122,41 @@ const BackHandler: TBackHandler = { /** * Listen for the `hardwareBackPress` event. The handler should return `true` * to prevent the event from bubbling to earlier registered listeners. + * + * Adding the same handler again reuses its listener: it keeps its place, + * and its request changes only if `intercept` is passed. */ addEventListener: function ( eventName: BackPressEventName, handler: BackPressHandler, - ): {remove: () => void, ...} { - if (_backPressSubscriptions.indexOf(handler) === -1) { - _backPressSubscriptions.push(handler); + options?: BackHandlerOptions, + ): BackHandlerSubscription { + let subscription: ?BackPressSubscription = _backPressSubscriptions.find( + existing => existing.handler === handler, + ); + if (subscription == null) { + const created: BackPressSubscription = {handler, intercept: false}; + _backPressSubscriptions.push(created); + subscription = created; + } + if (options?.intercept !== undefined) { + subscription.intercept = options.intercept === true; } + updateIntercept(); + + const registered = subscription; return { remove: (): void => { - const index = _backPressSubscriptions.indexOf(handler); + const index = _backPressSubscriptions.indexOf(registered); if (index !== -1) { _backPressSubscriptions.splice(index, 1); + updateIntercept(); } }, + setInterceptEnabled: (enabled: boolean): void => { + registered.intercept = enabled; + updateIntercept(); + }, }; }, }; diff --git a/packages/react-native/Libraries/Utilities/BackHandler.ios.js b/packages/react-native/Libraries/Utilities/BackHandler.ios.js index 605575e840c2..ddeaa31ba281 100644 --- a/packages/react-native/Libraries/Utilities/BackHandler.ios.js +++ b/packages/react-native/Libraries/Utilities/BackHandler.ios.js @@ -13,6 +13,16 @@ import type {HardwareBackPressEvent} from './HardwareBackPressEvent'; type BackPressEventName = 'backPress' | 'hardwareBackPress'; type BackPressHandler = (event: HardwareBackPressEvent) => ?boolean | void; +type BackHandlerOptions = { + intercept?: boolean, +}; + +type BackHandlerSubscription = { + remove: () => void, + setInterceptEnabled: (enabled: boolean) => void, + ... +}; + function emptyFunction(): void {} type TBackHandler = { @@ -20,14 +30,20 @@ type TBackHandler = { addEventListener( eventName: BackPressEventName, handler: BackPressHandler, - ): {remove: () => void, ...}, + options?: BackHandlerOptions, + ): BackHandlerSubscription, }; const BackHandler: TBackHandler = { exitApp: emptyFunction, - addEventListener(_eventName: BackPressEventName, _handler: BackPressHandler) { + addEventListener( + _eventName: BackPressEventName, + _handler: BackPressHandler, + _options?: BackHandlerOptions, + ) { return { remove: emptyFunction, + setInterceptEnabled: emptyFunction, }; }, }; diff --git a/packages/react-native/Libraries/Utilities/BackHandler.js.flow b/packages/react-native/Libraries/Utilities/BackHandler.js.flow index 591ac05bffda..3c444530f6dc 100644 --- a/packages/react-native/Libraries/Utilities/BackHandler.js.flow +++ b/packages/react-native/Libraries/Utilities/BackHandler.js.flow @@ -22,12 +22,38 @@ export interface HardwareBackPressEvent { readonly timeStamp: number; } +export type BackHandlerOptions = { + /** + * Android 16+ with targetSdk 36+ only. Ask Android to send back to React + * Native while this listener exists, instead of handling back itself with + * the predictive back animation. Defaults to `false`. + * + * It is a request, not a guarantee: native code that adds its own back + * callback after React Native's still receives back first. + */ + intercept?: boolean, +}; + +export type BackHandlerSubscription = { + remove: () => void, + /** + * Changes this listener's `intercept` request without removing it. + */ + setInterceptEnabled: (enabled: boolean) => void, + ... +}; + type TBackHandler = { exitApp(): void, + /** + * Adding the same handler again reuses its listener: it keeps its place, + * and its request changes only if `intercept` is passed. + */ addEventListener( eventName: BackPressEventName, handler: (event: HardwareBackPressEvent) => ?boolean | void, - ): {remove: () => void, ...}, + options?: BackHandlerOptions, + ): BackHandlerSubscription, }; /** diff --git a/packages/react-native/Libraries/Utilities/__mocks__/BackHandler.js b/packages/react-native/Libraries/Utilities/__mocks__/BackHandler.js index 9e964a069ba3..0b26d9c7628c 100644 --- a/packages/react-native/Libraries/Utilities/__mocks__/BackHandler.js +++ b/packages/react-native/Libraries/Utilities/__mocks__/BackHandler.js @@ -8,7 +8,11 @@ * @format */ -import type {BackPressEventName} from '../BackHandler'; +import type { + BackHandlerOptions, + BackHandlerSubscription, + BackPressEventName, +} from '../BackHandler'; import type {HardwareBackPressEvent} from '../HardwareBackPressEvent'; import {HardwareBackPressEvent as HardwareBackPressEventClass} from '../HardwareBackPressEvent'; @@ -23,12 +27,14 @@ const BackHandler = { addEventListener: function ( eventName: BackPressEventName, handler: (event: HardwareBackPressEvent) => ?boolean, - ): {remove: () => void, ...} { + _options?: BackHandlerOptions, + ): BackHandlerSubscription { _backPressSubscriptions.add(handler); return { remove: () => { _backPressSubscriptions.delete(handler); }, + setInterceptEnabled: (_enabled: boolean) => {}, }; }, diff --git a/packages/react-native/Libraries/Utilities/__tests__/BackHandler-itest.js b/packages/react-native/Libraries/Utilities/__tests__/BackHandler-itest.js index 2dfec3786236..5c6da225148f 100644 --- a/packages/react-native/Libraries/Utilities/__tests__/BackHandler-itest.js +++ b/packages/react-native/Libraries/Utilities/__tests__/BackHandler-itest.js @@ -142,3 +142,143 @@ describe('BackHandler', () => { expect(called).toBe(false); }); }); + +// Fantom has no DeviceEventManager native module, so the request BackHandler +// sends to native can't be observed. These cases cover the parts of the +// `intercept` option that are visible through the public API: it must not +// change which handlers run, or how often. +describe('BackHandler intercept option', () => { + const subscriptions: Array<{remove: () => void, ...}> = []; + + afterEach(() => { + for (const sub of subscriptions) { + sub.remove(); + } + subscriptions.length = 0; + }); + + it('calls a handler added with intercept', () => { + let called = false; + const handler = (_event: HardwareBackPressEventType) => { + called = true; + return true; + }; + + subscriptions.push( + BackHandler.addEventListener('hardwareBackPress', handler, { + intercept: true, + }), + ); + + DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100}); + + expect(called).toBe(true); + }); + + it('keeps reverse order across listeners with and without intercept', () => { + const callOrder: Array = []; + const handler1 = (_event: HardwareBackPressEventType) => { + callOrder.push('first'); + return false; + }; + const handler2 = (_event: HardwareBackPressEventType) => { + callOrder.push('second'); + return false; + }; + const handler3 = (_event: HardwareBackPressEventType) => { + callOrder.push('third'); + return false; + }; + + subscriptions.push( + BackHandler.addEventListener('hardwareBackPress', handler1, { + intercept: true, + }), + ); + subscriptions.push( + BackHandler.addEventListener('hardwareBackPress', handler2), + ); + subscriptions.push( + BackHandler.addEventListener('hardwareBackPress', handler3, { + intercept: true, + }), + ); + + DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100}); + + expect(callOrder).toEqual(['third', 'second', 'first']); + }); + + it('runs the same handlers after setInterceptEnabled', () => { + const callOrder: Array = []; + const intercepting = BackHandler.addEventListener( + 'hardwareBackPress', + (_event: HardwareBackPressEventType) => { + callOrder.push('intercepting'); + return false; + }, + {intercept: true}, + ); + const plain = BackHandler.addEventListener( + 'hardwareBackPress', + (_event: HardwareBackPressEventType) => { + callOrder.push('plain'); + return false; + }, + ); + subscriptions.push(intercepting, plain); + + intercepting.setInterceptEnabled(false); + plain.setInterceptEnabled(true); + DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100}); + + intercepting.setInterceptEnabled(true); + plain.setInterceptEnabled(false); + DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 200}); + + expect(callOrder).toEqual([ + 'plain', + 'intercepting', + 'plain', + 'intercepting', + ]); + }); + + it('does not bring back a removed listener on setInterceptEnabled', () => { + let called = false; + const subscription = BackHandler.addEventListener( + 'hardwareBackPress', + (_event: HardwareBackPressEventType) => { + called = true; + return true; + }, + ); + subscription.remove(); + + subscription.setInterceptEnabled(true); + DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100}); + + expect(called).toBe(false); + }); + + it('calls the same handler once when added twice', () => { + let calls = 0; + const handler = (_event: HardwareBackPressEventType) => { + calls++; + return false; + }; + + subscriptions.push( + BackHandler.addEventListener('hardwareBackPress', handler), + ); + subscriptions.push( + BackHandler.addEventListener('hardwareBackPress', handler, { + intercept: true, + }), + ); + + DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100}); + + expect(calls).toBe(1); + }); +}); diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 8e157a6367c5..08f22d5000b5 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -61,6 +61,7 @@ public final class com/facebook/react/NativeModuleRegistryBuilder { public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/AppCompatActivity, com/facebook/react/modules/core/DefaultHardwareBackBtnHandler, com/facebook/react/modules/core/PermissionAwareActivity { protected fun ()V protected fun createReactActivityDelegate ()Lcom/facebook/react/ReactActivityDelegate; + public fun getBackPressedCallback ()Landroidx/activity/OnBackPressedCallback; protected fun getMainComponentName ()Ljava/lang/String; public fun getReactActivityDelegate ()Lcom/facebook/react/ReactActivityDelegate; public fun getReactDelegate ()Lcom/facebook/react/ReactDelegate; @@ -84,6 +85,7 @@ public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/ public fun onUserLeaveHint ()V public fun onWindowFocusChanged (Z)V public fun requestPermissions ([Ljava/lang/String;ILcom/facebook/react/modules/core/PermissionListener;)V + public fun setInterceptEnabled (Z)V } public class com/facebook/react/ReactActivityDelegate { @@ -2535,6 +2537,7 @@ public class com/facebook/react/modules/core/DeviceEventManagerModule : com/face public fun emitHardwareBackPressed ()V public fun emitNewIntentReceived (Landroid/net/Uri;)V public fun invokeDefaultBackPressHandler ()V + public fun setInterceptEnabled (Z)V } public final class com/facebook/react/modules/core/DeviceEventManagerModule$Companion { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index b375d1859dbc..546fcc5e04cc 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -14,6 +14,8 @@ import androidx.activity.OnBackPressedCallback; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import com.facebook.react.modules.core.PermissionAwareActivity; import com.facebook.react.modules.core.PermissionListener; @@ -28,6 +30,9 @@ public abstract class ReactActivity extends AppCompatActivity // Due to enforced predictive back on targetSdk 36, 'onBackPressed()' is disabled by default. // Using a workaround to trigger it manually. + // With the enablePredictiveBackAndroid feature flag, the callback is disabled in onCreate, so + // Android handles back and plays its predictive back animation until JS (a BackHandler listener + // with `intercept: true`) or native code turns it on. private final OnBackPressedCallback mBackPressedCallback = new OnBackPressedCallback(true) { @Override @@ -38,6 +43,26 @@ public void handleOnBackPressed() { } }; + /** + * The callback React Native registers on Android 16+ with targetSdk 36+ to receive back. Native + * navigation libraries and brownfield hosts can disable it to handle back themselves. + */ + public OnBackPressedCallback getBackPressedCallback() { + return mBackPressedCallback; + } + + /** + * Called from JS while at least one BackHandler listener asks for back ({@code true}) or none + * does ({@code false}). Has no effect unless the enablePredictiveBackAndroid feature flag is on. + */ + public void setInterceptEnabled(boolean enabled) { + UiThreadUtil.assertOnUiThread(); + if (!ReactNativeFeatureFlags.enablePredictiveBackAndroid()) { + return; + } + mBackPressedCallback.setEnabled(enabled); + } + @SuppressWarnings("this-escape") protected ReactActivity() { mDelegate = createReactActivityDelegate(); @@ -61,6 +86,9 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDelegate.onCreate(savedInstanceState); if (AndroidVersion.isAtLeastTargetSdk36(this)) { + if (ReactNativeFeatureFlags.enablePredictiveBackAndroid()) { + mBackPressedCallback.setEnabled(false); + } getOnBackPressedDispatcher().addCallback(this, mBackPressedCallback); } } @@ -121,13 +149,16 @@ public void onBackPressed() { @Override public void invokeDefaultOnBackPressed() { + if (isFinishing()) { + return; + } // Disabling callback so the fallback logic (finish activity) can run // as super.onBackPressed() will call all enabled callbacks in the dispatcher. + boolean wasEnabled = mBackPressedCallback.isEnabled(); mBackPressedCallback.setEnabled(false); super.onBackPressed(); - // Re-enable callback to ensure custom back handling works after activity resume - // Without this, the callback remains disabled when the app returns from background - mBackPressedCallback.setEnabled(true); + // Restore the previous state: JS or native code may have disabled it on purpose. + mBackPressedCallback.setEnabled(wasEnabled); } @Override diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt index b581878cf589..ba8bde7ee879 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<99c0c14cf6e3eca24e7bf1c7eaf64d07>> + * @generated SignedSource<<8aa311770b06a39172d98a415b022321>> */ /** @@ -324,6 +324,12 @@ public object ReactNativeFeatureFlags { @JvmStatic public fun enablePreallocatedPropsDiffOnInsertAndroid(): Boolean = accessor.enablePreallocatedPropsDiffOnInsertAndroid() + /** + * On Android 16+ with targetSdk 36+, ReactActivity creates its back callback disabled so Android can play the predictive back animation. Back reaches JS only while a BackHandler listener asks for it with `intercept: true`. + */ + @JvmStatic + public fun enablePredictiveBackAndroid(): Boolean = accessor.enablePredictiveBackAndroid() + /** * Enables caching text layout artifacts for later reuse */ diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt index 22044709f9e4..e4c7828a2910 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -69,6 +69,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces private var enableMutationObserverByDefaultCache: Boolean? = null private var enableNativeCSSParsingCache: Boolean? = null private var enablePreallocatedPropsDiffOnInsertAndroidCache: Boolean? = null + private var enablePredictiveBackAndroidCache: Boolean? = null private var enablePreparedTextLayoutCache: Boolean? = null private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null private var enableResizeObserverByDefaultCache: Boolean? = null @@ -562,6 +563,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces return cached } + override fun enablePredictiveBackAndroid(): Boolean { + var cached = enablePredictiveBackAndroidCache + if (cached == null) { + cached = ReactNativeFeatureFlagsCxxInterop.enablePredictiveBackAndroid() + enablePredictiveBackAndroidCache = cached + } + return cached + } + override fun enablePreparedTextLayout(): Boolean { var cached = enablePreparedTextLayoutCache if (cached == null) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt index e68874e2401f..20cb591300a5 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<56b78da1741a04b8a526bd31030172e9>> */ /** @@ -126,6 +126,8 @@ public object ReactNativeFeatureFlagsCxxInterop { @DoNotStrip @JvmStatic public external fun enablePreallocatedPropsDiffOnInsertAndroid(): Boolean + @DoNotStrip @JvmStatic public external fun enablePredictiveBackAndroid(): Boolean + @DoNotStrip @JvmStatic public external fun enablePreparedTextLayout(): Boolean @DoNotStrip @JvmStatic public external fun enablePropsUpdateReconciliationAndroid(): Boolean diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt index d9e151c78a2a..3189b47ea73c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<26569f7cbc370133de7e8948ee2d4151>> + * @generated SignedSource<> */ /** @@ -121,6 +121,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi override fun enablePreallocatedPropsDiffOnInsertAndroid(): Boolean = false + override fun enablePredictiveBackAndroid(): Boolean = false + override fun enablePreparedTextLayout(): Boolean = true override fun enablePropsUpdateReconciliationAndroid(): Boolean = false diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt index a86b5a0757bb..9b596e923be1 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<5a3e2c98c831945e28a5ea0bc5a9eb70>> */ /** @@ -73,6 +73,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc private var enableMutationObserverByDefaultCache: Boolean? = null private var enableNativeCSSParsingCache: Boolean? = null private var enablePreallocatedPropsDiffOnInsertAndroidCache: Boolean? = null + private var enablePredictiveBackAndroidCache: Boolean? = null private var enablePreparedTextLayoutCache: Boolean? = null private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null private var enableResizeObserverByDefaultCache: Boolean? = null @@ -615,6 +616,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc return cached } + override fun enablePredictiveBackAndroid(): Boolean { + var cached = enablePredictiveBackAndroidCache + if (cached == null) { + cached = currentProvider.enablePredictiveBackAndroid() + accessedFeatureFlags.add("enablePredictiveBackAndroid") + enablePredictiveBackAndroidCache = cached + } + return cached + } + override fun enablePreparedTextLayout(): Boolean { var cached = enablePreparedTextLayoutCache if (cached == null) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt index 5475ffeb0647..fc98e026d5f6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<8e0059eadd2b19c49b64bb57121ba723>> + * @generated SignedSource<> */ /** @@ -121,6 +121,8 @@ public interface ReactNativeFeatureFlagsProvider { @DoNotStrip public fun enablePreallocatedPropsDiffOnInsertAndroid(): Boolean + @DoNotStrip public fun enablePredictiveBackAndroid(): Boolean + @DoNotStrip public fun enablePreparedTextLayout(): Boolean @DoNotStrip public fun enablePropsUpdateReconciliationAndroid(): Boolean diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt index 9ee720cf8bbc..b84c9cfab968 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt @@ -10,6 +10,7 @@ package com.facebook.react.modules.core import android.net.Uri import com.facebook.fbreact.specs.NativeDeviceEventManagerSpec import com.facebook.proguard.annotations.DoNotStripAny +import com.facebook.react.ReactActivity import com.facebook.react.bridge.JavaScriptModule import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.UiThreadUtil @@ -59,6 +60,16 @@ public open class DeviceEventManagerModule( reactApplicationContext.runOnUiQueueThread(invokeDefaultBackPressRunnable) } + /** + * Called by BackHandler whenever its listeners change, with whether at least one asks for back. + * Acts on the current activity if it is a [ReactActivity]. + */ + override fun setInterceptEnabled(enabled: Boolean) { + UiThreadUtil.runOnUiThread { + (reactApplicationContext.currentActivity as? ReactActivity)?.setInterceptEnabled(enabled) + } + } + public companion object { public const val NAME: String = NativeDeviceEventManagerSpec.NAME } diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp index 413d3cac7950..9408caebec62 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<53efcbcc0b7fb4f8da094449e96e1584>> + * @generated SignedSource<<7bce18c01d40cf5005d3a70278d6e500>> */ /** @@ -333,6 +333,12 @@ class ReactNativeFeatureFlagsJavaProvider return method(javaProvider_); } + bool enablePredictiveBackAndroid() override { + static const auto method = + getReactNativeFeatureFlagsProviderJavaClass()->getMethod("enablePredictiveBackAndroid"); + return method(javaProvider_); + } + bool enablePreparedTextLayout() override { static const auto method = getReactNativeFeatureFlagsProviderJavaClass()->getMethod("enablePreparedTextLayout"); @@ -888,6 +894,11 @@ bool JReactNativeFeatureFlagsCxxInterop::enablePreallocatedPropsDiffOnInsertAndr return ReactNativeFeatureFlags::enablePreallocatedPropsDiffOnInsertAndroid(); } +bool JReactNativeFeatureFlagsCxxInterop::enablePredictiveBackAndroid( + facebook::jni::alias_ref /*unused*/) { + return ReactNativeFeatureFlags::enablePredictiveBackAndroid(); +} + bool JReactNativeFeatureFlagsCxxInterop::enablePreparedTextLayout( facebook::jni::alias_ref /*unused*/) { return ReactNativeFeatureFlags::enablePreparedTextLayout(); @@ -1321,6 +1332,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() { makeNativeMethod( "enablePreallocatedPropsDiffOnInsertAndroid", JReactNativeFeatureFlagsCxxInterop::enablePreallocatedPropsDiffOnInsertAndroid), + makeNativeMethod( + "enablePredictiveBackAndroid", + JReactNativeFeatureFlagsCxxInterop::enablePredictiveBackAndroid), makeNativeMethod( "enablePreparedTextLayout", JReactNativeFeatureFlagsCxxInterop::enablePreparedTextLayout), diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h index 994940c07c27..281e6308b4bf 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -177,6 +177,9 @@ class JReactNativeFeatureFlagsCxxInterop static bool enablePreallocatedPropsDiffOnInsertAndroid( facebook::jni::alias_ref); + static bool enablePredictiveBackAndroid( + facebook::jni::alias_ref); + static bool enablePreparedTextLayout( facebook::jni::alias_ref); diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt new file mode 100644 index 000000000000..fb62af7a6ff7 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactActivityTest.kt @@ -0,0 +1,87 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react + +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class ReactActivityTest { + + // The callback registration in onCreate() isn't covered here: onCreate() needs a + // ReactApplication, and its SDK 36 check never passes with robolectric.properties at sdk=33. + class TestActivity : ReactActivity() + + private lateinit var activity: TestActivity + + @Before + fun setUp() { + ReactNativeFeatureFlagsForTests.setUp() + activity = Robolectric.buildActivity(TestActivity::class.java).get() + } + + @After + fun tearDown() { + ReactNativeFeatureFlags.dangerouslyReset() + } + + @Test + fun backPressedCallbackIsEnabledByDefault() { + assertThat(activity.backPressedCallback.isEnabled).isTrue() + } + + @Test + fun setInterceptEnabledHasNoEffectWithoutFeatureFlag() { + activity.setInterceptEnabled(false) + + assertThat(activity.backPressedCallback.isEnabled).isTrue() + } + + @Test + fun setInterceptEnabledTogglesCallbackWithFeatureFlag() { + enablePredictiveBack() + + activity.setInterceptEnabled(false) + assertThat(activity.backPressedCallback.isEnabled).isFalse() + + activity.setInterceptEnabled(true) + assertThat(activity.backPressedCallback.isEnabled).isTrue() + } + + @Test + fun invokeDefaultOnBackPressedRestoresDisabledState() { + activity.backPressedCallback.isEnabled = false + + activity.invokeDefaultOnBackPressed() + + assertThat(activity.backPressedCallback.isEnabled).isFalse() + } + + @Test + fun invokeDefaultOnBackPressedRestoresEnabledState() { + activity.invokeDefaultOnBackPressed() + + assertThat(activity.backPressedCallback.isEnabled).isTrue() + } + + private fun enablePredictiveBack() { + ReactNativeFeatureFlags.override( + object : ReactNativeFeatureFlagsDefaults() { + override fun enablePredictiveBackAndroid(): Boolean = true + }, + ) + } +} diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp index beb3e222c7e4..44ad051b2f5c 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -222,6 +222,10 @@ bool ReactNativeFeatureFlags::enablePreallocatedPropsDiffOnInsertAndroid() { return getAccessor().enablePreallocatedPropsDiffOnInsertAndroid(); } +bool ReactNativeFeatureFlags::enablePredictiveBackAndroid() { + return getAccessor().enablePredictiveBackAndroid(); +} + bool ReactNativeFeatureFlags::enablePreparedTextLayout() { return getAccessor().enablePreparedTextLayout(); } diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h index 4fa0141d7e80..4bc3a5bed827 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<51a639651d1427539ca5d3d07ae5e7b8>> */ /** @@ -286,6 +286,11 @@ class ReactNativeFeatureFlags { */ RN_EXPORT static bool enablePreallocatedPropsDiffOnInsertAndroid(); + /** + * On Android 16+ with targetSdk 36+, ReactActivity creates its back callback disabled so Android can play the predictive back animation. Back reaches JS only while a BackHandler listener asks for it with `intercept: true`. + */ + RN_EXPORT static bool enablePredictiveBackAndroid(); + /** * Enables caching text layout artifacts for later reuse */ diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp index 4316668001b7..93313bc9bc75 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<<45e6728b0fa599585a45b251b14a7b1f>> */ /** @@ -911,6 +911,24 @@ bool ReactNativeFeatureFlagsAccessor::enablePreallocatedPropsDiffOnInsertAndroid return flagValue.value(); } +bool ReactNativeFeatureFlagsAccessor::enablePredictiveBackAndroid() { + auto flagValue = enablePredictiveBackAndroid_.load(); + + if (!flagValue.has_value()) { + // This block is not exclusive but it is not necessary. + // If multiple threads try to initialize the feature flag, we would only + // be accessing the provider multiple times but the end state of this + // instance and the returned flag value would be the same. + + markFlagAsAccessed(49, "enablePredictiveBackAndroid"); + + flagValue = currentProvider_->enablePredictiveBackAndroid(); + enablePredictiveBackAndroid_ = flagValue; + } + + return flagValue.value(); +} + bool ReactNativeFeatureFlagsAccessor::enablePreparedTextLayout() { auto flagValue = enablePreparedTextLayout_.load(); @@ -920,7 +938,7 @@ bool ReactNativeFeatureFlagsAccessor::enablePreparedTextLayout() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(49, "enablePreparedTextLayout"); + markFlagAsAccessed(50, "enablePreparedTextLayout"); flagValue = currentProvider_->enablePreparedTextLayout(); enablePreparedTextLayout_ = flagValue; @@ -938,7 +956,7 @@ bool ReactNativeFeatureFlagsAccessor::enablePropsUpdateReconciliationAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(50, "enablePropsUpdateReconciliationAndroid"); + markFlagAsAccessed(51, "enablePropsUpdateReconciliationAndroid"); flagValue = currentProvider_->enablePropsUpdateReconciliationAndroid(); enablePropsUpdateReconciliationAndroid_ = flagValue; @@ -956,7 +974,7 @@ bool ReactNativeFeatureFlagsAccessor::enableResizeObserverByDefault() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(51, "enableResizeObserverByDefault"); + markFlagAsAccessed(52, "enableResizeObserverByDefault"); flagValue = currentProvider_->enableResizeObserverByDefault(); enableResizeObserverByDefault_ = flagValue; @@ -974,7 +992,7 @@ bool ReactNativeFeatureFlagsAccessor::enableSwiftUIBasedFilters() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(52, "enableSwiftUIBasedFilters"); + markFlagAsAccessed(53, "enableSwiftUIBasedFilters"); flagValue = currentProvider_->enableSwiftUIBasedFilters(); enableSwiftUIBasedFilters_ = flagValue; @@ -992,7 +1010,7 @@ bool ReactNativeFeatureFlagsAccessor::enableViewCulling() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(53, "enableViewCulling"); + markFlagAsAccessed(54, "enableViewCulling"); flagValue = currentProvider_->enableViewCulling(); enableViewCulling_ = flagValue; @@ -1010,7 +1028,7 @@ bool ReactNativeFeatureFlagsAccessor::enableViewRecycling() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(54, "enableViewRecycling"); + markFlagAsAccessed(55, "enableViewRecycling"); flagValue = currentProvider_->enableViewRecycling(); enableViewRecycling_ = flagValue; @@ -1028,7 +1046,7 @@ bool ReactNativeFeatureFlagsAccessor::enableViewRecyclingForImage() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(55, "enableViewRecyclingForImage"); + markFlagAsAccessed(56, "enableViewRecyclingForImage"); flagValue = currentProvider_->enableViewRecyclingForImage(); enableViewRecyclingForImage_ = flagValue; @@ -1046,7 +1064,7 @@ bool ReactNativeFeatureFlagsAccessor::enableViewRecyclingForScrollView() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(56, "enableViewRecyclingForScrollView"); + markFlagAsAccessed(57, "enableViewRecyclingForScrollView"); flagValue = currentProvider_->enableViewRecyclingForScrollView(); enableViewRecyclingForScrollView_ = flagValue; @@ -1064,7 +1082,7 @@ bool ReactNativeFeatureFlagsAccessor::enableViewRecyclingForText() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(57, "enableViewRecyclingForText"); + markFlagAsAccessed(58, "enableViewRecyclingForText"); flagValue = currentProvider_->enableViewRecyclingForText(); enableViewRecyclingForText_ = flagValue; @@ -1082,7 +1100,7 @@ bool ReactNativeFeatureFlagsAccessor::enableViewRecyclingForView() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(58, "enableViewRecyclingForView"); + markFlagAsAccessed(59, "enableViewRecyclingForView"); flagValue = currentProvider_->enableViewRecyclingForView(); enableViewRecyclingForView_ = flagValue; @@ -1100,7 +1118,7 @@ bool ReactNativeFeatureFlagsAccessor::enableVirtualViewContainerStateExperimenta // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(59, "enableVirtualViewContainerStateExperimental"); + markFlagAsAccessed(60, "enableVirtualViewContainerStateExperimental"); flagValue = currentProvider_->enableVirtualViewContainerStateExperimental(); enableVirtualViewContainerStateExperimental_ = flagValue; @@ -1118,7 +1136,7 @@ bool ReactNativeFeatureFlagsAccessor::fixBorderlessRippleAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(60, "fixBorderlessRippleAndroid"); + markFlagAsAccessed(61, "fixBorderlessRippleAndroid"); flagValue = currentProvider_->fixBorderlessRippleAndroid(); fixBorderlessRippleAndroid_ = flagValue; @@ -1136,7 +1154,7 @@ bool ReactNativeFeatureFlagsAccessor::fixMappingOfEventPrioritiesBetweenFabricAn // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(61, "fixMappingOfEventPrioritiesBetweenFabricAndReact"); + markFlagAsAccessed(62, "fixMappingOfEventPrioritiesBetweenFabricAndReact"); flagValue = currentProvider_->fixMappingOfEventPrioritiesBetweenFabricAndReact(); fixMappingOfEventPrioritiesBetweenFabricAndReact_ = flagValue; @@ -1154,7 +1172,7 @@ bool ReactNativeFeatureFlagsAccessor::fixProgrammaticScrollMomentumEventsIOS() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(62, "fixProgrammaticScrollMomentumEventsIOS"); + markFlagAsAccessed(63, "fixProgrammaticScrollMomentumEventsIOS"); flagValue = currentProvider_->fixProgrammaticScrollMomentumEventsIOS(); fixProgrammaticScrollMomentumEventsIOS_ = flagValue; @@ -1172,7 +1190,7 @@ bool ReactNativeFeatureFlagsAccessor::fixTextInlineViewLineHeight() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(63, "fixTextInlineViewLineHeight"); + markFlagAsAccessed(64, "fixTextInlineViewLineHeight"); flagValue = currentProvider_->fixTextInlineViewLineHeight(); fixTextInlineViewLineHeight_ = flagValue; @@ -1190,7 +1208,7 @@ bool ReactNativeFeatureFlagsAccessor::fixTextLayoutOnPixelDensityChange() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(64, "fixTextLayoutOnPixelDensityChange"); + markFlagAsAccessed(65, "fixTextLayoutOnPixelDensityChange"); flagValue = currentProvider_->fixTextLayoutOnPixelDensityChange(); fixTextLayoutOnPixelDensityChange_ = flagValue; @@ -1208,7 +1226,7 @@ bool ReactNativeFeatureFlagsAccessor::fixYogaFlexBasisFitContentInMainAxis() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(65, "fixYogaFlexBasisFitContentInMainAxis"); + markFlagAsAccessed(66, "fixYogaFlexBasisFitContentInMainAxis"); flagValue = currentProvider_->fixYogaFlexBasisFitContentInMainAxis(); fixYogaFlexBasisFitContentInMainAxis_ = flagValue; @@ -1226,7 +1244,7 @@ bool ReactNativeFeatureFlagsAccessor::fuseboxAssertSingleHostState() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(66, "fuseboxAssertSingleHostState"); + markFlagAsAccessed(67, "fuseboxAssertSingleHostState"); flagValue = currentProvider_->fuseboxAssertSingleHostState(); fuseboxAssertSingleHostState_ = flagValue; @@ -1244,7 +1262,7 @@ bool ReactNativeFeatureFlagsAccessor::fuseboxEnabledRelease() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(67, "fuseboxEnabledRelease"); + markFlagAsAccessed(68, "fuseboxEnabledRelease"); flagValue = currentProvider_->fuseboxEnabledRelease(); fuseboxEnabledRelease_ = flagValue; @@ -1262,7 +1280,7 @@ bool ReactNativeFeatureFlagsAccessor::fuseboxFrameRecordingEnabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(68, "fuseboxFrameRecordingEnabled"); + markFlagAsAccessed(69, "fuseboxFrameRecordingEnabled"); flagValue = currentProvider_->fuseboxFrameRecordingEnabled(); fuseboxFrameRecordingEnabled_ = flagValue; @@ -1280,7 +1298,7 @@ bool ReactNativeFeatureFlagsAccessor::fuseboxScreenshotCaptureEnabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(69, "fuseboxScreenshotCaptureEnabled"); + markFlagAsAccessed(70, "fuseboxScreenshotCaptureEnabled"); flagValue = currentProvider_->fuseboxScreenshotCaptureEnabled(); fuseboxScreenshotCaptureEnabled_ = flagValue; @@ -1298,7 +1316,7 @@ bool ReactNativeFeatureFlagsAccessor::fuseboxWebSocketEventsEnabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(70, "fuseboxWebSocketEventsEnabled"); + markFlagAsAccessed(71, "fuseboxWebSocketEventsEnabled"); flagValue = currentProvider_->fuseboxWebSocketEventsEnabled(); fuseboxWebSocketEventsEnabled_ = flagValue; @@ -1316,7 +1334,7 @@ bool ReactNativeFeatureFlagsAccessor::keepTimersRunningWhileHostPausedAndroid() // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(71, "keepTimersRunningWhileHostPausedAndroid"); + markFlagAsAccessed(72, "keepTimersRunningWhileHostPausedAndroid"); flagValue = currentProvider_->keepTimersRunningWhileHostPausedAndroid(); keepTimersRunningWhileHostPausedAndroid_ = flagValue; @@ -1334,7 +1352,7 @@ bool ReactNativeFeatureFlagsAccessor::optimizedAnimatedPropUpdates() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(72, "optimizedAnimatedPropUpdates"); + markFlagAsAccessed(73, "optimizedAnimatedPropUpdates"); flagValue = currentProvider_->optimizedAnimatedPropUpdates(); optimizedAnimatedPropUpdates_ = flagValue; @@ -1352,7 +1370,7 @@ bool ReactNativeFeatureFlagsAccessor::overrideBySynchronousMountPropsAtMountingA // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(73, "overrideBySynchronousMountPropsAtMountingAndroid"); + markFlagAsAccessed(74, "overrideBySynchronousMountPropsAtMountingAndroid"); flagValue = currentProvider_->overrideBySynchronousMountPropsAtMountingAndroid(); overrideBySynchronousMountPropsAtMountingAndroid_ = flagValue; @@ -1370,7 +1388,7 @@ bool ReactNativeFeatureFlagsAccessor::perfIssuesEnabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(74, "perfIssuesEnabled"); + markFlagAsAccessed(75, "perfIssuesEnabled"); flagValue = currentProvider_->perfIssuesEnabled(); perfIssuesEnabled_ = flagValue; @@ -1388,7 +1406,7 @@ bool ReactNativeFeatureFlagsAccessor::perfMonitorV2Enabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(75, "perfMonitorV2Enabled"); + markFlagAsAccessed(76, "perfMonitorV2Enabled"); flagValue = currentProvider_->perfMonitorV2Enabled(); perfMonitorV2Enabled_ = flagValue; @@ -1406,7 +1424,7 @@ double ReactNativeFeatureFlagsAccessor::preparedTextCacheSize() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(76, "preparedTextCacheSize"); + markFlagAsAccessed(77, "preparedTextCacheSize"); flagValue = currentProvider_->preparedTextCacheSize(); preparedTextCacheSize_ = flagValue; @@ -1424,7 +1442,7 @@ bool ReactNativeFeatureFlagsAccessor::preventShadowTreeCommitExhaustion() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(77, "preventShadowTreeCommitExhaustion"); + markFlagAsAccessed(78, "preventShadowTreeCommitExhaustion"); flagValue = currentProvider_->preventShadowTreeCommitExhaustion(); preventShadowTreeCommitExhaustion_ = flagValue; @@ -1442,7 +1460,7 @@ bool ReactNativeFeatureFlagsAccessor::redBoxV2Android() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(78, "redBoxV2Android"); + markFlagAsAccessed(79, "redBoxV2Android"); flagValue = currentProvider_->redBoxV2Android(); redBoxV2Android_ = flagValue; @@ -1460,7 +1478,7 @@ bool ReactNativeFeatureFlagsAccessor::redBoxV2IOS() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(79, "redBoxV2IOS"); + markFlagAsAccessed(80, "redBoxV2IOS"); flagValue = currentProvider_->redBoxV2IOS(); redBoxV2IOS_ = flagValue; @@ -1478,7 +1496,7 @@ bool ReactNativeFeatureFlagsAccessor::shouldPressibilityUseW3CPointerEventsForHo // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(80, "shouldPressibilityUseW3CPointerEventsForHover"); + markFlagAsAccessed(81, "shouldPressibilityUseW3CPointerEventsForHover"); flagValue = currentProvider_->shouldPressibilityUseW3CPointerEventsForHover(); shouldPressibilityUseW3CPointerEventsForHover_ = flagValue; @@ -1496,7 +1514,7 @@ bool ReactNativeFeatureFlagsAccessor::shouldTriggerResponderTransferOnScrollAndr // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(81, "shouldTriggerResponderTransferOnScrollAndroid"); + markFlagAsAccessed(82, "shouldTriggerResponderTransferOnScrollAndroid"); flagValue = currentProvider_->shouldTriggerResponderTransferOnScrollAndroid(); shouldTriggerResponderTransferOnScrollAndroid_ = flagValue; @@ -1514,7 +1532,7 @@ bool ReactNativeFeatureFlagsAccessor::skipActivityIdentityAssertionOnHostPause() // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(82, "skipActivityIdentityAssertionOnHostPause"); + markFlagAsAccessed(83, "skipActivityIdentityAssertionOnHostPause"); flagValue = currentProvider_->skipActivityIdentityAssertionOnHostPause(); skipActivityIdentityAssertionOnHostPause_ = flagValue; @@ -1532,7 +1550,7 @@ bool ReactNativeFeatureFlagsAccessor::skipUnchangedChildrenInDifferentiator() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(83, "skipUnchangedChildrenInDifferentiator"); + markFlagAsAccessed(84, "skipUnchangedChildrenInDifferentiator"); flagValue = currentProvider_->skipUnchangedChildrenInDifferentiator(); skipUnchangedChildrenInDifferentiator_ = flagValue; @@ -1550,7 +1568,7 @@ bool ReactNativeFeatureFlagsAccessor::syncAndroidClipBoundsWithOverflow() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(84, "syncAndroidClipBoundsWithOverflow"); + markFlagAsAccessed(85, "syncAndroidClipBoundsWithOverflow"); flagValue = currentProvider_->syncAndroidClipBoundsWithOverflow(); syncAndroidClipBoundsWithOverflow_ = flagValue; @@ -1568,7 +1586,7 @@ bool ReactNativeFeatureFlagsAccessor::traceTurboModulePromiseRejectionsOnAndroid // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(85, "traceTurboModulePromiseRejectionsOnAndroid"); + markFlagAsAccessed(86, "traceTurboModulePromiseRejectionsOnAndroid"); flagValue = currentProvider_->traceTurboModulePromiseRejectionsOnAndroid(); traceTurboModulePromiseRejectionsOnAndroid_ = flagValue; @@ -1586,7 +1604,7 @@ bool ReactNativeFeatureFlagsAccessor::updateRuntimeShadowNodeReferencesOnCommit( // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(86, "updateRuntimeShadowNodeReferencesOnCommit"); + markFlagAsAccessed(87, "updateRuntimeShadowNodeReferencesOnCommit"); flagValue = currentProvider_->updateRuntimeShadowNodeReferencesOnCommit(); updateRuntimeShadowNodeReferencesOnCommit_ = flagValue; @@ -1604,7 +1622,7 @@ bool ReactNativeFeatureFlagsAccessor::updateRuntimeShadowNodeReferencesOnCommitT // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(87, "updateRuntimeShadowNodeReferencesOnCommitThread"); + markFlagAsAccessed(88, "updateRuntimeShadowNodeReferencesOnCommitThread"); flagValue = currentProvider_->updateRuntimeShadowNodeReferencesOnCommitThread(); updateRuntimeShadowNodeReferencesOnCommitThread_ = flagValue; @@ -1622,7 +1640,7 @@ bool ReactNativeFeatureFlagsAccessor::useAlwaysAvailableJSErrorHandling() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(88, "useAlwaysAvailableJSErrorHandling"); + markFlagAsAccessed(89, "useAlwaysAvailableJSErrorHandling"); flagValue = currentProvider_->useAlwaysAvailableJSErrorHandling(); useAlwaysAvailableJSErrorHandling_ = flagValue; @@ -1640,7 +1658,7 @@ bool ReactNativeFeatureFlagsAccessor::useFabricInterop() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(89, "useFabricInterop"); + markFlagAsAccessed(90, "useFabricInterop"); flagValue = currentProvider_->useFabricInterop(); useFabricInterop_ = flagValue; @@ -1658,7 +1676,7 @@ bool ReactNativeFeatureFlagsAccessor::useNativeViewConfigsInBridgelessMode() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(90, "useNativeViewConfigsInBridgelessMode"); + markFlagAsAccessed(91, "useNativeViewConfigsInBridgelessMode"); flagValue = currentProvider_->useNativeViewConfigsInBridgelessMode(); useNativeViewConfigsInBridgelessMode_ = flagValue; @@ -1676,7 +1694,7 @@ bool ReactNativeFeatureFlagsAccessor::useNestedScrollViewAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(91, "useNestedScrollViewAndroid"); + markFlagAsAccessed(92, "useNestedScrollViewAndroid"); flagValue = currentProvider_->useNestedScrollViewAndroid(); useNestedScrollViewAndroid_ = flagValue; @@ -1694,7 +1712,7 @@ bool ReactNativeFeatureFlagsAccessor::useSharedAnimatedBackend() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(92, "useSharedAnimatedBackend"); + markFlagAsAccessed(93, "useSharedAnimatedBackend"); flagValue = currentProvider_->useSharedAnimatedBackend(); useSharedAnimatedBackend_ = flagValue; @@ -1712,7 +1730,7 @@ bool ReactNativeFeatureFlagsAccessor::useTraitHiddenOnAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(93, "useTraitHiddenOnAndroid"); + markFlagAsAccessed(94, "useTraitHiddenOnAndroid"); flagValue = currentProvider_->useTraitHiddenOnAndroid(); useTraitHiddenOnAndroid_ = flagValue; @@ -1730,7 +1748,7 @@ bool ReactNativeFeatureFlagsAccessor::useTurboModuleInterop() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(94, "useTurboModuleInterop"); + markFlagAsAccessed(95, "useTurboModuleInterop"); flagValue = currentProvider_->useTurboModuleInterop(); useTurboModuleInterop_ = flagValue; @@ -1748,7 +1766,7 @@ bool ReactNativeFeatureFlagsAccessor::useWindowDensityForDisplayMetricsAndroid() // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(95, "useWindowDensityForDisplayMetricsAndroid"); + markFlagAsAccessed(96, "useWindowDensityForDisplayMetricsAndroid"); flagValue = currentProvider_->useWindowDensityForDisplayMetricsAndroid(); useWindowDensityForDisplayMetricsAndroid_ = flagValue; @@ -1766,7 +1784,7 @@ double ReactNativeFeatureFlagsAccessor::viewCullingOutsetRatio() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(96, "viewCullingOutsetRatio"); + markFlagAsAccessed(97, "viewCullingOutsetRatio"); flagValue = currentProvider_->viewCullingOutsetRatio(); viewCullingOutsetRatio_ = flagValue; @@ -1784,7 +1802,7 @@ bool ReactNativeFeatureFlagsAccessor::viewTransitionEnabled() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(97, "viewTransitionEnabled"); + markFlagAsAccessed(98, "viewTransitionEnabled"); flagValue = currentProvider_->viewTransitionEnabled(); viewTransitionEnabled_ = flagValue; @@ -1802,7 +1820,7 @@ bool ReactNativeFeatureFlagsAccessor::viewTransitionUseHardwareBitmapAndroid() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(98, "viewTransitionUseHardwareBitmapAndroid"); + markFlagAsAccessed(99, "viewTransitionUseHardwareBitmapAndroid"); flagValue = currentProvider_->viewTransitionUseHardwareBitmapAndroid(); viewTransitionUseHardwareBitmapAndroid_ = flagValue; @@ -1820,7 +1838,7 @@ double ReactNativeFeatureFlagsAccessor::virtualViewPrerenderRatio() { // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. - markFlagAsAccessed(99, "virtualViewPrerenderRatio"); + markFlagAsAccessed(100, "virtualViewPrerenderRatio"); flagValue = currentProvider_->virtualViewPrerenderRatio(); virtualViewPrerenderRatio_ = flagValue; diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h index 83b43a289ece..4a51268f7c33 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsAccessor.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<721aba8fb57d190d560886c966a1561b>> + * @generated SignedSource<<7d184da03532473a5c52c86763dad60f>> */ /** @@ -83,6 +83,7 @@ class ReactNativeFeatureFlagsAccessor { bool enableMutationObserverByDefault(); bool enableNativeCSSParsing(); bool enablePreallocatedPropsDiffOnInsertAndroid(); + bool enablePredictiveBackAndroid(); bool enablePreparedTextLayout(); bool enablePropsUpdateReconciliationAndroid(); bool enableResizeObserverByDefault(); @@ -145,7 +146,7 @@ class ReactNativeFeatureFlagsAccessor { std::unique_ptr currentProvider_; bool wasOverridden_; - std::array, 100> accessedFeatureFlags_; + std::array, 101> accessedFeatureFlags_; std::atomic> commonTestFlag_; std::atomic> cdpInteractionMetricsEnabled_; @@ -196,6 +197,7 @@ class ReactNativeFeatureFlagsAccessor { std::atomic> enableMutationObserverByDefault_; std::atomic> enableNativeCSSParsing_; std::atomic> enablePreallocatedPropsDiffOnInsertAndroid_; + std::atomic> enablePredictiveBackAndroid_; std::atomic> enablePreparedTextLayout_; std::atomic> enablePropsUpdateReconciliationAndroid_; std::atomic> enableResizeObserverByDefault_; diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h index 7b48631860be..4b93061ac37a 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDefaults.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -225,6 +225,10 @@ class ReactNativeFeatureFlagsDefaults : public ReactNativeFeatureFlagsProvider { return false; } + bool enablePredictiveBackAndroid() override { + return false; + } + bool enablePreparedTextLayout() override { return true; } diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h index 8aae155500c7..ed15f0991751 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsDynamicProvider.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ /** @@ -488,6 +488,15 @@ class ReactNativeFeatureFlagsDynamicProvider : public ReactNativeFeatureFlagsDef return ReactNativeFeatureFlagsDefaults::enablePreallocatedPropsDiffOnInsertAndroid(); } + bool enablePredictiveBackAndroid() override { + auto value = values_["enablePredictiveBackAndroid"]; + if (!value.isNull()) { + return value.getBool(); + } + + return ReactNativeFeatureFlagsDefaults::enablePredictiveBackAndroid(); + } + bool enablePreparedTextLayout() override { auto value = values_["enablePreparedTextLayout"]; if (!value.isNull()) { diff --git a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h index ca24ec0c7a9d..9102b134282c 100644 --- a/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h +++ b/packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlagsProvider.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<2b7964e40792fa3438a17bfeb92e4140>> + * @generated SignedSource<<10a537823852bb21fc3fc78b6a0b9a47>> */ /** @@ -76,6 +76,7 @@ class ReactNativeFeatureFlagsProvider { virtual bool enableMutationObserverByDefault() = 0; virtual bool enableNativeCSSParsing() = 0; virtual bool enablePreallocatedPropsDiffOnInsertAndroid() = 0; + virtual bool enablePredictiveBackAndroid() = 0; virtual bool enablePreparedTextLayout() = 0; virtual bool enablePropsUpdateReconciliationAndroid() = 0; virtual bool enableResizeObserverByDefault() = 0; diff --git a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp index 9a23d96f05b9..6506e8c12e2f 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.cpp @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<8669d5fd41e7808f6344dd9bba1c09a7>> + * @generated SignedSource<> */ /** @@ -289,6 +289,11 @@ bool NativeReactNativeFeatureFlags::enablePreallocatedPropsDiffOnInsertAndroid( return ReactNativeFeatureFlags::enablePreallocatedPropsDiffOnInsertAndroid(); } +bool NativeReactNativeFeatureFlags::enablePredictiveBackAndroid( + jsi::Runtime& /*runtime*/) { + return ReactNativeFeatureFlags::enablePredictiveBackAndroid(); +} + bool NativeReactNativeFeatureFlags::enablePreparedTextLayout( jsi::Runtime& /*runtime*/) { return ReactNativeFeatureFlags::enablePreparedTextLayout(); diff --git a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h index 83ac2d03511f..a47ebe13990e 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h +++ b/packages/react-native/ReactCommon/react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<23d1a2b0fa50a9a8d138dc1b06288b24>> + * @generated SignedSource<> */ /** @@ -136,6 +136,8 @@ class NativeReactNativeFeatureFlags bool enablePreallocatedPropsDiffOnInsertAndroid(jsi::Runtime& runtime); + bool enablePredictiveBackAndroid(jsi::Runtime& runtime); + bool enablePreparedTextLayout(jsi::Runtime& runtime); bool enablePropsUpdateReconciliationAndroid(jsi::Runtime& runtime); diff --git a/packages/react-native/ReactNativeApi.d.ts b/packages/react-native/ReactNativeApi.d.ts index 1b1fcc723561..8857348c7614 100644 --- a/packages/react-native/ReactNativeApi.d.ts +++ b/packages/react-native/ReactNativeApi.d.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<1844fb63742cd2547a287716f2881ac4>> + * @generated SignedSource<<97637b6feb9c83e4e7aa9c24f7df2115>> * * This file was generated by scripts/js-api/build-types/index.js. */ @@ -1643,6 +1643,13 @@ declare type BackgroundSizeValue = { y: number | string } declare type BackHandler = typeof BackHandler +declare type BackHandlerOptions = { + intercept?: boolean +} +declare type BackHandlerSubscription = { + remove: () => void + setInterceptEnabled: (enabled: boolean) => void +} declare type BackPressEventName = "backPress" | "hardwareBackPress" declare type BaseKeyboardEvent = { duration: number @@ -4950,9 +4957,8 @@ declare type TBackHandler = { addEventListener( eventName: BackPressEventName, handler: (event: HardwareBackPressEvent) => (boolean | undefined) | void, - ): { - remove: () => void - } + options?: BackHandlerOptions, + ): BackHandlerSubscription exitApp(): void } declare type Text = typeof Text @@ -5902,7 +5908,9 @@ export { AssetDestPathResolver, // 59047424 AssetRegistry, // 6070bb45 AutoCapitalize, // c0e857a0 - BackHandler, // 29f7039a + BackHandler, // a81e4e07 + BackHandlerOptions, // e9af377e + BackHandlerSubscription, // 082347e4 BackPressEventName, // 4620fb76 BlurEvent, // 00c596e2 BoxShadowValue, // b679703f diff --git a/packages/react-native/index.js.flow b/packages/react-native/index.js.flow index d1397e29920b..113d30c59a0a 100644 --- a/packages/react-native/index.js.flow +++ b/packages/react-native/index.js.flow @@ -295,7 +295,11 @@ export type { PackagerAsset, } from './src/private/assets/AssetRegistry'; -export type {BackPressEventName} from './Libraries/Utilities/BackHandler'; +export type { + BackHandlerOptions, + BackHandlerSubscription, + BackPressEventName, +} from './Libraries/Utilities/BackHandler'; export {default as BackHandler} from './Libraries/Utilities/BackHandler'; export {default as codegenNativeComponent} from './Libraries/Utilities/codegenNativeComponent'; diff --git a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js index 3cbba444da49..5cf067973b57 100644 --- a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js +++ b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js @@ -577,6 +577,16 @@ const definitions: FeatureFlagDefinitions = { }, ossReleaseStage: 'none', }, + enablePredictiveBackAndroid: { + defaultValue: false, + metadata: { + description: + 'On Android 16+ with targetSdk 36+, ReactActivity creates its back callback disabled so Android can play the predictive back animation. Back reaches JS only while a BackHandler listener asks for it with `intercept: true`.', + expectedReleaseValue: true, + purpose: 'release', + }, + ossReleaseStage: 'none', + }, enablePreparedTextLayout: { defaultValue: true, metadata: { diff --git a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js index fae658e77472..ac94ef4517fe 100644 --- a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> * @flow strict * @noformat */ @@ -97,6 +97,7 @@ export type ReactNativeFeatureFlags = Readonly<{ enableMutationObserverByDefault: Getter, enableNativeCSSParsing: Getter, enablePreallocatedPropsDiffOnInsertAndroid: Getter, + enablePredictiveBackAndroid: Getter, enablePreparedTextLayout: Getter, enablePropsUpdateReconciliationAndroid: Getter, enableResizeObserverByDefault: Getter, @@ -415,6 +416,10 @@ export const enableNativeCSSParsing: Getter = createNativeFlagGetter('e * When enabled together with `enableAccumulatedUpdatesInRawPropsAndroid`, the Insert of a preallocated view only sends the difference between the props the view was preallocated with and the inserted props, instead of sending all props again on every Insert. */ export const enablePreallocatedPropsDiffOnInsertAndroid: Getter = createNativeFlagGetter('enablePreallocatedPropsDiffOnInsertAndroid', false); +/** + * On Android 16+ with targetSdk 36+, ReactActivity creates its back callback disabled so Android can play the predictive back animation. Back reaches JS only while a BackHandler listener asks for it with `intercept: true`. + */ +export const enablePredictiveBackAndroid: Getter = createNativeFlagGetter('enablePredictiveBackAndroid', false); /** * Enables caching text layout artifacts for later reuse */ diff --git a/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js index d7e6b22a7cf3..d7ba4fc2c6fc 100644 --- a/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<9b9925a584fbe2922611bc75655e9401>> + * @generated SignedSource<<526d36856aec874a946e4f04560f74c4>> * @flow strict * @noformat */ @@ -74,6 +74,7 @@ export interface Spec extends TurboModule { readonly enableMutationObserverByDefault?: () => boolean; readonly enableNativeCSSParsing?: () => boolean; readonly enablePreallocatedPropsDiffOnInsertAndroid?: () => boolean; + readonly enablePredictiveBackAndroid?: () => boolean; readonly enablePreparedTextLayout?: () => boolean; readonly enablePropsUpdateReconciliationAndroid?: () => boolean; readonly enableResizeObserverByDefault?: () => boolean; diff --git a/packages/react-native/src/private/specs_DEPRECATED/modules/NativeDeviceEventManager.js b/packages/react-native/src/private/specs_DEPRECATED/modules/NativeDeviceEventManager.js index 4efff76e66a0..98ad054746cb 100644 --- a/packages/react-native/src/private/specs_DEPRECATED/modules/NativeDeviceEventManager.js +++ b/packages/react-native/src/private/specs_DEPRECATED/modules/NativeDeviceEventManager.js @@ -14,6 +14,7 @@ import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboMod export interface Spec extends TurboModule { readonly invokeDefaultBackPressHandler: () => void; + readonly setInterceptEnabled?: (enabled: boolean) => void; } export default TurboModuleRegistry.get('DeviceEventManager') as ?Spec; diff --git a/packages/react-native/types_DEPRECATED/Libraries/Utilities/BackHandler.d.ts b/packages/react-native/types_DEPRECATED/Libraries/Utilities/BackHandler.d.ts index ac2a530a9c05..6617a3eabe72 100644 --- a/packages/react-native/types_DEPRECATED/Libraries/Utilities/BackHandler.d.ts +++ b/packages/react-native/types_DEPRECATED/Libraries/Utilities/BackHandler.d.ts @@ -32,12 +32,37 @@ export interface HardwareBackPressEvent { * * @see https://reactnative.dev/docs/backhandler */ +export interface BackHandlerOptions { + /** + * Android 16+ with targetSdk 36+ only. Ask Android to send back to React + * Native while this listener exists, instead of handling back itself with + * the predictive back animation. React Native intercepts back while at + * least one listener asks for it. Defaults to `false`. + * + * It is a request, not a guarantee: native code that adds its own back + * callback after React Native's still receives back first. + */ + intercept?: boolean | undefined; +} + +export interface BackHandlerSubscription extends NativeEventSubscription { + /** + * Changes this listener's `intercept` request without removing it. + */ + setInterceptEnabled(enabled: boolean): void; +} + export interface BackHandlerStatic { exitApp(): void; + /** + * Adding the same handler again reuses its listener: it keeps its place, + * and its request changes only if `intercept` is passed. + */ addEventListener( eventName: BackPressEventName, handler: (event: HardwareBackPressEvent) => boolean | null | undefined, - ): NativeEventSubscription; + options?: BackHandlerOptions, + ): BackHandlerSubscription; } export const BackHandler: BackHandlerStatic; diff --git a/packages/rn-tester/js/RNTesterAppShared.js b/packages/rn-tester/js/RNTesterAppShared.js index 1c2ced536e94..412454431c8c 100644 --- a/packages/rn-tester/js/RNTesterAppShared.js +++ b/packages/rn-tester/js/RNTesterAppShared.js @@ -134,6 +134,9 @@ const RNTesterApp = ({ const subscription = BackHandler.addEventListener( 'hardwareBackPress', handleHardwareBackPress, + // Ask for back only while an example is open. On the examples list, + // Android handles back and plays its predictive back animation. + {intercept: activeModuleKey != null}, ); return () => subscription.remove(); }, [activeModuleKey, handleBackPress]);