From bb3ec4d6c342d3afcb109e8c65f99e7693c962b5 Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Thu, 12 Sep 2024 18:13:51 -0700 Subject: [PATCH] Add protected constructor to decouple ReactFragment lifecycle events from ReactHost (#46425) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46425 It's entirely plausible that one would want to run a ReactFragment that does not tear down the ReactHost when it is destroyed. This adds a protected constructor that can be used to disable host lifecycle events in a ReactFragment. When disabled, the `onDestroy` method of the ReactFragment simply stops the surface attached to it using `ReactDelegate.unloadApp`. ## Changelog [Android][Added] Flag in ReactFragment to allow unmounting a surface without destroying ReactHost. Reviewed By: philIip Differential Revision: D62449779 fbshipit-source-id: a443ed004ced45e0adc95bd43b36d1b80f9f392f --- .../ReactAndroid/api/ReactAndroid.api | 1 + .../com/facebook/react/ReactFragment.java | 24 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 4d97920dd2a..c7f223a1353 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -188,6 +188,7 @@ public class com/facebook/react/ReactFragment : androidx/fragment/app/Fragment, protected static final field ARG_LAUNCH_OPTIONS Ljava/lang/String; protected field mReactDelegate Lcom/facebook/react/ReactDelegate; public fun ()V + protected fun (Z)V public fun checkPermission (Ljava/lang/String;II)I public fun checkSelfPermission (Ljava/lang/String;)I protected fun getReactDelegate ()Lcom/facebook/react/ReactDelegate; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactFragment.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactFragment.java index 12f2d9daba8..1ee220200df 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactFragment.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactFragment.java @@ -32,10 +32,20 @@ public class ReactFragment extends Fragment implements PermissionAwareActivity { protected ReactDelegate mReactDelegate; + private final boolean mDisableHostLifecycleEvents; + @Nullable private PermissionListener mPermissionListener; public ReactFragment() { // Required empty public constructor + this(false); + } + + /** + * @param disableHostLifecycleEvents Disable forwarding lifecycle events to the {@link ReactHost}. + */ + protected ReactFragment(boolean disableHostLifecycleEvents) { + this.mDisableHostLifecycleEvents = disableHostLifecycleEvents; } /** @@ -99,19 +109,27 @@ public class ReactFragment extends Fragment implements PermissionAwareActivity { @Override public void onResume() { super.onResume(); - mReactDelegate.onHostResume(); + if (!mDisableHostLifecycleEvents) { + mReactDelegate.onHostResume(); + } } @Override public void onPause() { super.onPause(); - mReactDelegate.onHostPause(); + if (!mDisableHostLifecycleEvents) { + mReactDelegate.onHostPause(); + } } @Override public void onDestroy() { super.onDestroy(); - mReactDelegate.onHostDestroy(); + if (!mDisableHostLifecycleEvents) { + mReactDelegate.onHostDestroy(); + } else { + mReactDelegate.unloadApp(); + } } // endregion