Skip to content

Commit

Permalink
Add protected constructor to decouple ReactFragment lifecycle events …
Browse files Browse the repository at this point in the history
…from ReactHost (#46425)

Summary:
Pull Request resolved: #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
  • Loading branch information
rozele authored and facebook-github-bot committed Sep 13, 2024
1 parent cf8d09b commit bb3ec4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -99,19 +109,27 @@ public View onCreateView(
@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
Expand Down

0 comments on commit bb3ec4d

Please sign in to comment.