From 7b3698f7b1f0f8cfdd99c0e5af2720ef9bf8a2ae Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Tue, 5 Nov 2024 23:32:43 +0800 Subject: [PATCH] Fix android reload reentrant issue --- .../facebook/react/devsupport/DevSupportManagerBase.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java index dcfa599094b1e8..528b057b1c3351 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -61,6 +61,7 @@ import com.facebook.react.devsupport.interfaces.StackFrame; import com.facebook.react.modules.core.RCTNativeAppEventEmitter; import com.facebook.react.modules.debug.interfaces.DeveloperSettings; +import com.facebook.react.packagerconnection.JSPackagerClient; import com.facebook.react.packagerconnection.RequestHandler; import com.facebook.react.packagerconnection.Responder; import java.io.File; @@ -363,6 +364,12 @@ private int getExponentActivityId() { @Override public void reloadExpoApp() { try { + if (mDevServerHelper.mPackagerClient != null) { + // In Expo Go's multi-Activity structure, reloading means destroying the current Activity and creating a new one with a fresh React instance. + // To prevent reentrant `reloadExpoApp` from being triggered by a long press of the "r" key in the CLI, which could lead to an unexpected state, + // we must terminate the packager connection immediately. This is done without waiting a worker thread by using `mDevServerHelper.closePackagerConnection()`. + mDevServerHelper.mPackagerClient.close(); + } Class.forName("host.exp.exponent.ReactNativeStaticHelpers").getMethod("reloadFromManifest", int.class).invoke(null, getExponentActivityId()); } catch (Exception expoHandleErrorException) { expoHandleErrorException.printStackTrace();