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
This commit is contained in:
Eric Rozell
2024-09-12 18:13:51 -07:00
committed by Facebook GitHub Bot
parent cf8d09b279
commit bb3ec4d6c3
2 changed files with 22 additions and 3 deletions
@@ -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 <init> ()V
protected fun <init> (Z)V
public fun checkPermission (Ljava/lang/String;II)I
public fun checkSelfPermission (Ljava/lang/String;)I
protected fun getReactDelegate ()Lcom/facebook/react/ReactDelegate;
@@ -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