Skip to content

Commit

Permalink
[0.76] Fix ReactFragment on New Architecture (#46675)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico authored Sep 30, 2024
1 parent 2f04dfe commit 52322fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,7 @@ public void loadApp(String appKey) {
// With Bridgeless enabled, create and start the surface
if (ReactFeatureFlags.enableBridgelessArchitecture) {
if (mReactSurface == null) {
// Create a ReactSurface
mReactSurface = mReactHost.createSurface(mActivity, appKey, mLaunchOptions);
// Set main Activity's content view
mActivity.setContentView(mReactSurface.getView());
}
mReactSurface.start();
} else {
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.config.ReactFeatureFlags;
import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;

Expand Down Expand Up @@ -69,9 +70,15 @@ 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 (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactDelegate =
new ReactDelegate(
getActivity(), getReactHost(), mainComponentName, launchOptions);
} else {
mReactDelegate =
new ReactDelegate(
getActivity(), getReactNativeHost(), mainComponentName, launchOptions, fabricEnabled);
}
}

/**
Expand All @@ -81,8 +88,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 52322fb

Please sign in to comment.