Skip to content

Commit

Permalink
React Activity exposes ReactHost (#46980)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46980

Changelog: [Android][Added]  React Activity exposes ReactHost

**Context:**

*These changes will not impact `DefaultReactHost` which is the way most OSS apps interface with `ReactHost`*

* We are simplifying `ReactHost` and `ReactHostDelegate` for Brownfield uses.
* We fetch the `ReactHost` to create the `ReactDelegate`
https://www.internalfb.com/code/fbsource/[00ee07afc695]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java?lines=110-112

* With Bridgeless, you must use or extend `ReactHostDelegate` to get the `ReactHost` since we don't expose a getter on `ReactActivity`.
* If there is a custom Application, then getting the `ReactHost` will need a custom implementation.
* For the base case, we shouldn't need to subclass the delegate.

**Change:**

* Expose `ReactHost` on `ReactActivity` for Bridgeless access.
* Expose `ReactActivity` on `ReactActivityDelegate`. This will help us avoid keeping a reference to Activity in each subclass.
* Update the RN Android API's

Reviewed By: arushikesarwani94

Differential Revision: D64150994

fbshipit-source-id: a9d790a4b4ce4eca29dee4bdc8d9fc56cf742f1f
  • Loading branch information
shwanton authored and facebook-github-bot committed Oct 18, 2024
1 parent 398512a commit d78cb78
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/
protected fun getMainComponentName ()Ljava/lang/String;
public fun getReactActivityDelegate ()Lcom/facebook/react/ReactActivityDelegate;
public fun getReactDelegate ()Lcom/facebook/react/ReactDelegate;
protected fun getReactHost ()Lcom/facebook/react/ReactHost;
protected final fun getReactInstanceManager ()Lcom/facebook/react/ReactInstanceManager;
protected final fun getReactNativeHost ()Lcom/facebook/react/ReactNativeHost;
public fun invokeDefaultOnBackPressed ()V
Expand Down Expand Up @@ -127,6 +128,7 @@ public class com/facebook/react/ReactActivityDelegate {
protected fun getLaunchOptions ()Landroid/os/Bundle;
public fun getMainComponentName ()Ljava/lang/String;
protected fun getPlainActivity ()Landroid/app/Activity;
protected fun getReactActivity ()Lcom/facebook/react/ReactActivity;
protected fun getReactDelegate ()Lcom/facebook/react/ReactDelegate;
public fun getReactHost ()Lcom/facebook/react/ReactHost;
public fun getReactInstanceManager ()Lcom/facebook/react/ReactInstanceManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ protected final ReactNativeHost getReactNativeHost() {
return mDelegate.getReactNativeHost();
}

protected ReactHost getReactHost() {
return mDelegate.getReactHost();
}

protected final ReactInstanceManager getReactInstanceManager() {
return mDelegate.getReactInstanceManager();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public ReactActivityDelegate(
}

/**
* Get the {@link ReactNativeHost} used by this app. By default, assumes {@link
* Activity#getApplication()} is an instance of {@link ReactApplication} and calls {@link
* Get the {@link ReactNativeHost} used by this app with Bridge enabled. By default, assumes
* {@link Activity#getApplication()} is an instance of {@link ReactApplication} and calls {@link
* ReactApplication#getReactNativeHost()}. Override this method if your application class does not
* implement {@code ReactApplication} or you simply have a different mechanism for storing a
* {@code ReactNativeHost}, e.g. as a static field somewhere.
Expand All @@ -86,6 +86,13 @@ protected ReactNativeHost getReactNativeHost() {
return ((ReactApplication) getPlainActivity().getApplication()).getReactNativeHost();
}

/**
* Get the {@link ReactHost} used by this app with Bridgeless enabled. 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.
*/
public ReactHost getReactHost() {
return ((ReactApplication) getPlainActivity().getApplication()).getReactHost();
}
Expand Down Expand Up @@ -226,6 +233,10 @@ protected Activity getPlainActivity() {
return ((Activity) getContext());
}

protected ReactActivity getReactActivity() {
return ((ReactActivity) getContext());
}

/**
* 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.
Expand Down

0 comments on commit d78cb78

Please sign in to comment.