diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index acff375ddea..16be4f9f398 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -109,6 +109,7 @@ public class com/facebook/react/ReactActivityDelegate { protected fun composeLaunchOptions ()Landroid/os/Bundle; protected fun createRootView ()Lcom/facebook/react/ReactRootView; protected fun getContext ()Landroid/content/Context; + public fun getCurrentReactContext ()Lcom/facebook/react/bridge/ReactContext; protected fun getLaunchOptions ()Landroid/os/Bundle; public fun getMainComponentName ()Ljava/lang/String; protected fun getPlainActivity ()Landroid/app/Activity; @@ -147,6 +148,8 @@ public class com/facebook/react/ReactDelegate { public fun (Landroid/app/Activity;Lcom/facebook/react/ReactNativeHost;Ljava/lang/String;Landroid/os/Bundle;)V public fun (Landroid/app/Activity;Lcom/facebook/react/ReactNativeHost;Ljava/lang/String;Landroid/os/Bundle;Z)V protected fun createRootView ()Lcom/facebook/react/ReactRootView; + public fun getCurrentReactContext ()Lcom/facebook/react/bridge/ReactContext; + public fun getReactHost ()Lcom/facebook/react/ReactHost; public fun getReactInstanceManager ()Lcom/facebook/react/ReactInstanceManager; public fun getReactRootView ()Lcom/facebook/react/ReactRootView; protected fun isFabricEnabled ()Z diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java index 766aa856cd8..1560b269b38 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java @@ -18,6 +18,8 @@ import android.view.KeyEvent; import androidx.annotation.Nullable; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReactContext; +import com.facebook.react.common.annotations.DeprecatedInNewArchitecture; import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags; import com.facebook.react.modules.core.PermissionListener; import com.facebook.systrace.Systrace; @@ -82,6 +84,7 @@ public class ReactActivityDelegate { * implement {@code ReactApplication} or you simply have a different mechanism for storing a * {@code ReactNativeHost}, e.g. as a static field somewhere. */ + @DeprecatedInNewArchitecture(message = "Use getReactHost()") protected ReactNativeHost getReactNativeHost() { return ((ReactApplication) getPlainActivity().getApplication()).getReactNativeHost(); } @@ -93,7 +96,7 @@ public class ReactActivityDelegate { * implement {@code ReactApplication} or you simply have a different mechanism for storing a * {@code ReactHost}, e.g. as a static field somewhere. */ - public ReactHost getReactHost() { + public @Nullable ReactHost getReactHost() { return ((ReactApplication) getPlainActivity().getApplication()).getReactHost(); } @@ -101,6 +104,7 @@ public class ReactActivityDelegate { return mReactDelegate; } + @DeprecatedInNewArchitecture(message = "Use getReactHost()") public ReactInstanceManager getReactInstanceManager() { return mReactDelegate.getReactInstanceManager(); } @@ -237,6 +241,16 @@ public class ReactActivityDelegate { return ((ReactActivity) getContext()); } + /** + * Get the current {@link ReactContext} from ReactHost or ReactInstanceManager + * + *

Do not store a reference to this, if the React instance is reloaded or destroyed, this + * context will no longer be valid. + */ + public @Nullable ReactContext getCurrentReactContext() { + return mReactDelegate.getCurrentReactContext(); + } + /** * Override this method if you wish to selectively toggle Fabric for a specific surface. This will * also control if Concurrent Root (React 18) should be enabled or not. diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java index 983526a5e36..b418bad2613 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java @@ -14,7 +14,9 @@ import android.os.Bundle; import android.view.KeyEvent; import androidx.annotation.Nullable; import com.facebook.infer.annotation.Assertions; +import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.common.annotations.DeprecatedInNewArchitecture; import com.facebook.react.devsupport.DoubleTapReloadRecognizer; import com.facebook.react.devsupport.ReleaseDevSupportManager; import com.facebook.react.devsupport.interfaces.DevSupportManager; @@ -365,14 +367,38 @@ public class ReactDelegate { } /** Get the {@link ReactNativeHost} used by this app. */ + @DeprecatedInNewArchitecture(message = "Use getReactHost()") private ReactNativeHost getReactNativeHost() { return mReactNativeHost; } + @DeprecatedInNewArchitecture(message = "Use getReactHost()") public ReactInstanceManager getReactInstanceManager() { return getReactNativeHost().getReactInstanceManager(); } + public @Nullable ReactHost getReactHost() { + return mReactHost; + } + + /** + * Get the current {@link ReactContext} from ReactHost or ReactInstanceManager + * + *

Do not store a reference to this, if the React instance is reloaded or destroyed, this + * context will no longer be valid. + */ + public @Nullable ReactContext getCurrentReactContext() { + if (ReactNativeFeatureFlags.enableBridgelessArchitecture()) { + if (mReactHost != null) { + return mReactHost.getCurrentReactContext(); + } else { + return null; + } + } else { + return getReactInstanceManager().getCurrentReactContext(); + } + } + /** * Override this method if you wish to selectively toggle Fabric for a specific surface. This will * also control if Concurrent Root (React 18) should be enabled or not.