From 627b779f6bef5706b01dec1707a9e68354033a26 Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Wed, 24 Apr 2024 02:22:55 +0800 Subject: [PATCH] fix ReactActivity.getReactDelegate().reload() --- .../com/facebook/react/ReactActivity.java | 4 +-- .../com/facebook/react/ReactDelegate.java | 31 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index 45306772da1244..b68be38edd1316 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -65,8 +65,8 @@ protected void onDestroy() { mDelegate.onDestroy(); } - public void getReactDelegate() { - mDelegate.getReactDelegate(); + public ReactDelegate getReactDelegate() { + return mDelegate.getReactDelegate(); } public ReactActivityDelegate getReactActivityDelegate() { 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..d791824e1d6c6a 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,29 @@ 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() {