Expose an API to enable Concurrent Root on Android (#33645)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/33645

With React 18, we now need to allow users on Fabric to opt-in for Concurrent Root.

This commit adds a new method that can be called on the ReactActivityDelegate
that can be used to set the `concurrentRoot` flag on the `initialProps` on the Render.

Changelog:
[Android] [Added] - Expose an API to enable Concurrent Root on Android

Reviewed By: mdvacca

Differential Revision: D35614879

fbshipit-source-id: 2de83e8115d3748c0346cdec6f31b2ab1f899478
This commit is contained in:
Nicola Corti
2022-04-20 08:03:21 -07:00
committed by Facebook GitHub Bot
parent a12959546a
commit d7b64b8d4b
4 changed files with 107 additions and 1 deletions
@@ -15,6 +15,7 @@ import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Callback;
@@ -33,6 +34,7 @@ public class ReactActivityDelegate {
private @Nullable PermissionListener mPermissionListener;
private @Nullable Callback mPermissionsCallback;
private ReactDelegate mReactDelegate;
private boolean mConcurrentRootEnabled;
@Deprecated
public ReactActivityDelegate(Activity activity, @Nullable String mainComponentName) {
@@ -45,10 +47,27 @@ public class ReactActivityDelegate {
mMainComponentName = mainComponentName;
}
/**
* Public API to populate the launch options that will be passed to React. Here you can customize
* the values that will be passed as `initialProperties` to the Renderer.
*
* @return Either null or a key-value map as a Bundle
*/
protected @Nullable Bundle getLaunchOptions() {
return null;
}
private @NonNull Bundle composeLaunchOptions() {
Bundle composedLaunchOptions = getLaunchOptions();
if (isConcurrentRootEnabled()) {
if (composedLaunchOptions == null) {
composedLaunchOptions = new Bundle();
}
composedLaunchOptions.putBoolean("concurrentRoot", true);
}
return composedLaunchOptions;
}
protected ReactRootView createRootView() {
return new ReactRootView(getContext());
}
@@ -74,9 +93,10 @@ public class ReactActivityDelegate {
protected void onCreate(Bundle savedInstanceState) {
String mainComponentName = getMainComponentName();
Bundle launchOptions = composeLaunchOptions();
mReactDelegate =
new ReactDelegate(
getPlainActivity(), getReactNativeHost(), mainComponentName, getLaunchOptions()) {
getPlainActivity(), getReactNativeHost(), mainComponentName, launchOptions) {
@Override
protected ReactRootView createRootView() {
return ReactActivityDelegate.this.createRootView();
@@ -190,4 +210,16 @@ public class ReactActivityDelegate {
protected Activity getPlainActivity() {
return ((Activity) getContext());
}
/**
* Override this method to enable Concurrent Root on the surface for this Activity. See:
* https://reactjs.org/blog/2022/03/29/react-v18.html
*
* <p>This requires to be rendering on Fabric (i.e. on the New Architecture).
*
* @return Wether you want to enable Concurrent Root for this surface or not.
*/
protected boolean isConcurrentRootEnabled() {
return false;
}
}