Skip to content

Commit

Permalink
Add unloadApp method to ReactDelegate (#46424)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46424

It's not isomorphic for `ReactDelegate` to have a `loadApp` method to initialize a surface, but only allow unloading of a surface by calling `onHostDestroy`, which also destroys the ReactHost. This change adds an `unloadApp` method to `ReactDelegate` so it can be used in, e.g., `ReactFragment` to stop a surface without tearing down the host.

## Changelog

[Android][Added] ReactDelegate `unloadApp` methods for unmounting surfaces without destroying ReactHost

Reviewed By: mdvacca

Differential Revision: D62448543

fbshipit-source-id: e99b880f48e857935ee856c37b881bcc8bd22ad0
  • Loading branch information
rozele authored and facebook-github-bot committed Sep 12, 2024
1 parent cbddcfc commit 38593c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 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 @@ -179,6 +179,7 @@ public class com/facebook/react/ReactDelegate {
public fun onWindowFocusChanged (Z)V
public fun reload ()V
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
public fun unloadApp ()V
}

public class com/facebook/react/ReactFragment : androidx/fragment/app/Fragment, com/facebook/react/modules/core/PermissionAwareActivity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,10 @@ public void onHostPause() {
}

public void onHostDestroy() {
unloadApp();
if (ReactFeatureFlags.enableBridgelessArchitecture) {
if (mReactSurface != null) {
mReactSurface.stop();
mReactSurface = null;
}
mReactHost.onHostDestroy(mActivity);
} else {
if (mReactRootView != null) {
mReactRootView.unmountReactApplication();
mReactRootView = null;
}
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onHostDestroy(mActivity);
}
Expand Down Expand Up @@ -281,10 +274,16 @@ public void reload() {
devSupportManager.handleReloadJS();
}

/** Start the React surface with the app key supplied in the {@link ReactDelegate} constructor. */
public void loadApp() {
loadApp(mMainComponentName);
}

/**
* Start the React surface for the given app key.
*
* @param appKey The ID of the app to load into the surface.
*/
public void loadApp(String appKey) {
// With Bridgeless enabled, create and start the surface
if (ReactFeatureFlags.enableBridgelessArchitecture) {
Expand All @@ -305,6 +304,21 @@ public void loadApp(String appKey) {
}
}

/** Stop the React surface started with {@link ReactDelegate#loadApp()}. */
public void unloadApp() {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
if (mReactSurface != null) {
mReactSurface.stop();
mReactSurface = null;
}
} else {
if (mReactRootView != null) {
mReactRootView.unmountReactApplication();
mReactRootView = null;
}
}
}

@Nullable
public ReactRootView getReactRootView() {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
Expand Down

0 comments on commit 38593c4

Please sign in to comment.