From 79e5e64b9a8fdc370150b2e81501fc8ac0a22dfe Mon Sep 17 00:00:00 2001 From: Jane Li Date: Wed, 3 Apr 2024 08:18:46 -0700 Subject: [PATCH] RN changes to enable fragment-based navigation [1/3] (#43789) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43789 This diff introduces some changes to React Native needed to enable fragment-based navigation (see next diff for usage). Fragment-based navigation will enable us to, instead of re-creating the entire main activity on navigation, create a fragment instead - allowing us to bypass a lot of unnecessary onCreate() logic in our main activity to improve user experience and performance. Changelog: [Internal] [Changed] - Add option to skip calling delegate on ReactFragment lifecycle events Reviewed By: keoskate Differential Revision: D55646221 fbshipit-source-id: 45b148cb9ecdb1484f1ac714ac2b93ce09c52237 --- .../react-native/ReactAndroid/api/ReactAndroid.api | 2 ++ .../java/com/facebook/react/ReactActivity.java | 4 ++++ .../java/com/facebook/react/ReactFragment.java | 14 +++++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index bba39b5d0ecb48..4500e16bb86c34 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -92,6 +92,7 @@ public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/ protected fun ()V protected fun createReactActivityDelegate ()Lcom/facebook/react/ReactActivityDelegate; protected fun getMainComponentName ()Ljava/lang/String; + public fun getReactActivityDelegate ()Lcom/facebook/react/ReactActivityDelegate; public fun getReactDelegate ()V protected final fun getReactInstanceManager ()Lcom/facebook/react/ReactInstanceManager; protected final fun getReactNativeHost ()Lcom/facebook/react/ReactNativeHost; @@ -178,6 +179,7 @@ public class com/facebook/react/ReactFragment : androidx/fragment/app/Fragment, protected static final field ARG_FABRIC_ENABLED Ljava/lang/String; protected static final field ARG_LAUNCH_OPTIONS Ljava/lang/String; protected field mReactDelegate Lcom/facebook/react/ReactDelegate; + protected field shouldCallDelegateLifecycleEvents Z public fun ()V public fun checkPermission (Ljava/lang/String;II)I public fun checkSelfPermission (Ljava/lang/String;)I 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 ef827a4f16a165..45306772da1244 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 @@ -69,6 +69,10 @@ public void getReactDelegate() { mDelegate.getReactDelegate(); } + public ReactActivityDelegate getReactActivityDelegate() { + return mDelegate; + } + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); 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 12f2d9daba822b..b61b66f8fcf1bb 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,6 +32,8 @@ public class ReactFragment extends Fragment implements PermissionAwareActivity { protected ReactDelegate mReactDelegate; + protected boolean shouldCallDelegateLifecycleEvents = true; + @Nullable private PermissionListener mPermissionListener; public ReactFragment() { @@ -99,19 +101,25 @@ public View onCreateView( @Override public void onResume() { super.onResume(); - mReactDelegate.onHostResume(); + if (shouldCallDelegateLifecycleEvents) { + mReactDelegate.onHostResume(); + } } @Override public void onPause() { super.onPause(); - mReactDelegate.onHostPause(); + if (shouldCallDelegateLifecycleEvents) { + mReactDelegate.onHostPause(); + } } @Override public void onDestroy() { super.onDestroy(); - mReactDelegate.onHostDestroy(); + if (shouldCallDelegateLifecycleEvents) { + mReactDelegate.onHostDestroy(); + } } // endregion