Skip to content

Commit

Permalink
ReactFragment should properly instantiate ReactDelegate on Bridgeless (
Browse files Browse the repository at this point in the history
…#46623)

Summary:
Pull Request resolved: #46623

I've just noticed that ReactFragment is not properly instantiating the `ReactDelegate` with a ReactHost
when on Bridgeless. This causes Fragments to crash when the app is on bridgeless mode.

Fixes #46566

Changelog:
[Android] [Fixed] - ReactFragment should properly instantiate ReactDelegate on Bridgeless

Reviewed By: mdvacca

Differential Revision: D63319977

fbshipit-source-id: 08256e35b2769e18df2d24f870ec5d98e5574f85
  • Loading branch information
cortinico authored and facebook-github-bot committed Sep 27, 2024
1 parent ecd6609 commit 7176d11
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 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 @@ -192,6 +192,7 @@ public class com/facebook/react/ReactFragment : androidx/fragment/app/Fragment,
public fun checkPermission (Ljava/lang/String;II)I
public fun checkSelfPermission (Ljava/lang/String;)I
protected fun getReactDelegate ()Lcom/facebook/react/ReactDelegate;
protected fun getReactHost ()Lcom/facebook/react/ReactHost;
protected fun getReactNativeHost ()Lcom/facebook/react/ReactNativeHost;
public fun onActivityResult (IILandroid/content/Intent;)V
public fun onBackPressed ()Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags;
import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;

Expand Down Expand Up @@ -80,9 +81,14 @@ public void onCreate(Bundle savedInstanceState) {
if (mainComponentName == null) {
throw new IllegalStateException("Cannot loadApp if component name is null");
}
mReactDelegate =
new ReactDelegate(
getActivity(), getReactNativeHost(), mainComponentName, launchOptions, fabricEnabled);
if (ReactNativeFeatureFlags.enableBridgelessArchitecture()) {
mReactDelegate =
new ReactDelegate(getActivity(), getReactHost(), mainComponentName, launchOptions);
} else {
mReactDelegate =
new ReactDelegate(
getActivity(), getReactNativeHost(), mainComponentName, launchOptions, fabricEnabled);
}
}

/**
Expand All @@ -92,8 +98,34 @@ public void onCreate(Bundle savedInstanceState) {
* implement {@code ReactApplication} or you simply have a different mechanism for storing a
* {@code ReactNativeHost}, e.g. as a static field somewhere.
*/
@Nullable
protected ReactNativeHost getReactNativeHost() {
return ((ReactApplication) getActivity().getApplication()).getReactNativeHost();
ReactApplication application = ((ReactApplication) getActivity().getApplication());
if (application != null) {
return application.getReactNativeHost();
} else {
return null;
}
}

/**
* Get the {@link ReactHost} used by this app. By default, assumes {@link
* Activity#getApplication()} is an instance of {@link ReactApplication} and calls {@link
* ReactApplication#getReactHost()}. Override this method if your application class does not
* implement {@code ReactApplication} or you simply have a different mechanism for storing a
* {@code ReactHost}, e.g. as a static field somewhere.
*
* <p>If you're using Old Architecture/Bridge Mode, this method should return null as {@link
* ReactHost} is a Bridgeless-only concept.
*/
@Nullable
protected ReactHost getReactHost() {
ReactApplication application = ((ReactApplication) getActivity().getApplication());
if (application != null) {
return application.getReactHost();
} else {
return null;
}
}

protected ReactDelegate getReactDelegate() {
Expand Down

0 comments on commit 7176d11

Please sign in to comment.