From 539922339b5ca1937ce3207038199a27566914fe 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 ## Changelog: [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 6aeb8f8aff8181..4007b823ff9cac 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,6 +14,7 @@ import android.view.KeyEvent; 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.DoubleTapReloadRecognizer; import com.facebook.react.devsupport.ReleaseDevSupportManager; @@ -98,7 +99,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; @@ -237,17 +238,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 ReleaseDevSupportManager - && 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() {