From 56631cd471008c39fe6f771052c3f1af7085bd63 Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Wed, 1 May 2024 15:21:44 -0700 Subject: [PATCH] fix ReactActivity.getReactDelegate().reload() (#44223) Summary: fixing some problem for `ReactActivity.getReactDelegate().reload()` from https://github.com/facebook/react-native/issues/43521: - the `reload()` does not work for bridge mode on release build [ANDROID] [FIXED] - Fixed app reloading for `ReactActivity.getReactDelegate().reload()`. Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../com/facebook/react/ReactDelegate.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) 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 3afe3d96af23e8..9ce067b9fc56dc 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 @@ -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; @@ -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; @@ -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() {