Skip to content

Commit

Permalink
fix ReactActivity.getReactDelegate().reload() (#44223)
Browse files Browse the repository at this point in the history
Summary:
fixing some problem for `ReactActivity.getReactDelegate().reload()` from #43521:
- the `reload()` does not work for bridge mode on release build

[ANDROID] [FIXED] - Fixed app reloading for `ReactActivity.getReactDelegate().reload()`.

Pull Request resolved: #44223

Test Plan:
tried to temporary change toast.show as reload and test from rn-tester
```diff
 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/toast/ToastModule.kt
@@ -10,6 +10,7 @@ package com.facebook.react.modules.toast
 import android.view.Gravity
 import android.widget.Toast
 import com.facebook.fbreact.specs.NativeToastAndroidSpec
+import com.facebook.react.ReactActivity
 import com.facebook.react.bridge.NativeModule
 import com.facebook.react.bridge.ReactApplicationContext
 import com.facebook.react.bridge.UiThreadUtil
@@ -30,9 +31,11 @@ public class ToastModule(reactContext: ReactApplicationContext) :
       )

   override public fun show(message: String?, durationDouble: Double) {
-    val duration = durationDouble.toInt()
-    UiThreadUtil.runOnUiThread(
-        Runnable { Toast.makeText(getReactApplicationContext(), message, duration).show() })
+//    val duration = durationDouble.toInt()
+//    UiThreadUtil.runOnUiThread(
+//        Runnable { Toast.makeText(getReactApplicationContext(), message, duration).show() })
+    val activity = reactApplicationContext.currentActivity as? ReactActivity
+    activity?.reactDelegate?.reload()
   }

   override public fun showWithGravity(
```

tried for different mode
- [x] bridge mode + debug build
- [x] bridgeless mode + debug build
- [x] bridge mode + release build
- [x] bridgeless mode + release build

Reviewed By: fkgozali

Differential Revision: D56795975

Pulled By: arushikesarwani94

fbshipit-source-id: 895eab1927ba6db748ebb32c0fd5313f19cf9d1b
  • Loading branch information
Kudo authored and cipolleschi committed May 2, 2024
1 parent cc1c697 commit 56631cd
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.devsupport.DisabledDevSupportManager;
import com.facebook.react.devsupport.DoubleTapReloadRecognizer;
Expand Down Expand Up @@ -99,7 +100,7 @@ private DevSupportManager getDevSupportManager() {
&& mReactHost.getDevSupportManager() != null) {
return mReactHost.getDevSupportManager();
} else if (getReactNativeHost().hasInstance()
&& getReactNativeHost().getUseDeveloperSupport()) {
&& getReactNativeHost().getReactInstanceManager() != null) {
return getReactNativeHost().getReactInstanceManager().getDevSupportManager();
} else {
return null;
Expand Down Expand Up @@ -241,17 +242,31 @@ public boolean onKeyLongPress(int keyCode) {

public void reload() {
DevSupportManager devSupportManager = getDevSupportManager();
if (devSupportManager != null) {
// With Bridgeless enabled, reload in RELEASE mode
if (devSupportManager instanceof DisabledDevSupportManager
&& ReactFeatureFlags.enableBridgelessArchitecture
&& mReactHost != null) {
// Do not reload the bundle from JS as there is no bundler running in release mode.
mReactHost.reload("ReactDelegate.reload()");
if (devSupportManager == null) {
return;
}

// Reload in RELEASE mode
if (devSupportManager instanceof ReleaseDevSupportManager) {
// Do not reload the bundle from JS as there is no bundler running in release mode.
if (ReactFeatureFlags.enableBridgelessArchitecture) {
if (mReactHost != null) {
mReactHost.reload("ReactDelegate.reload()");
}
} else {
devSupportManager.handleReloadJS();
UiThreadUtil.runOnUiThread(
() -> {
if (mReactNativeHost.hasInstance()
&& mReactNativeHost.getReactInstanceManager() != null) {
mReactNativeHost.getReactInstanceManager().recreateReactContextInBackground();
}
});
}
return;
}

// Reload in DEBUG mode
devSupportManager.handleReloadJS();
}

public void loadApp() {
Expand Down

0 comments on commit 56631cd

Please sign in to comment.