Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit eccf892

Browse files
authored
Merge pull request #11 from hudl/CleanUpInstanceMgrIfNotUsed
Clean-up RN Instance Manager
2 parents 990d9e7 + 0b7721f commit eccf892

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

react-native-android-fragment/src/main/java/com/hudl/oss/react/fragment/ReactFragment.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737

3838
import com.facebook.infer.annotation.Assertions;
3939
import com.facebook.react.ReactApplication;
40+
import com.facebook.react.ReactInstanceManager;
4041
import com.facebook.react.ReactNativeHost;
4142
import com.facebook.react.ReactRootView;
43+
import com.facebook.react.common.LifecycleState;
4244
import com.facebook.react.devsupport.DoubleTapReloadRecognizer;
4345
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
4446
import com.facebook.react.modules.core.PermissionAwareActivity;
@@ -152,7 +154,15 @@ public void onDestroy() {
152154
mReactRootView = null;
153155
}
154156
if (getReactNativeHost().hasInstance()) {
155-
getReactNativeHost().getReactInstanceManager().onHostDestroy(getActivity());
157+
ReactInstanceManager reactInstanceMgr = getReactNativeHost().getReactInstanceManager();
158+
reactInstanceMgr.onHostDestroy(getActivity());
159+
160+
// onDestroy may be called on a ReactFragment after another ReactFragment has been
161+
// created and resumed with the same React Instance Manager. Make sure we only clean up
162+
// host's React Instance Manager if no other React Fragment is actively using it.
163+
if (reactInstanceMgr.getLifecycleState() != LifecycleState.RESUMED) {
164+
getReactNativeHost().clear();
165+
}
156166
}
157167
}
158168

0 commit comments

Comments
 (0)