Skip to content

Commit d792121

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Clean listeners during destroy of ReactContext
Summary: This diff cleans listeners on the destruction of the ReactContext. changelog: [inernal] internal Reviewed By: JoshuaGross Differential Revision: D26259929 fbshipit-source-id: 1843cabdac2fa3e67dcc890afd923b82472d8f66
1 parent 98165a2 commit d792121

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/JSIModuleRegistry.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.bridge;
99

1010
import com.facebook.infer.annotation.Assertions;
11+
import com.facebook.react.config.ReactFeatureFlags;
1112
import java.util.HashMap;
1213
import java.util.List;
1314
import java.util.Map;
@@ -44,6 +45,8 @@ public void notifyJSInstanceDestroy() {
4445
JSIModuleHolder moduleHolder = entry.getValue();
4546
moduleHolder.notifyJSInstanceDestroy();
4647
}
47-
mModules.clear();
48+
if (ReactFeatureFlags.enableReactContextCleanupFix) {
49+
mModules.clear();
50+
}
4851
}
4952
}

ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.bridge;
99

1010
import com.facebook.infer.annotation.Assertions;
11+
import com.facebook.react.config.ReactFeatureFlags;
1112
import com.facebook.react.module.annotations.ReactModule;
1213
import com.facebook.systrace.Systrace;
1314
import java.util.ArrayList;
@@ -84,7 +85,9 @@ private ReactApplicationContext getReactApplicationContext() {
8485
for (ModuleHolder module : mModules.values()) {
8586
module.destroy();
8687
}
87-
mModules.clear();
88+
if (ReactFeatureFlags.enableReactContextCleanupFix) {
89+
mModules.clear();
90+
}
8891
} finally {
8992
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
9093
}

ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
2424
import com.facebook.react.common.LifecycleState;
2525
import com.facebook.react.common.ReactConstants;
26+
import com.facebook.react.config.ReactFeatureFlags;
2627
import java.lang.ref.WeakReference;
2728
import java.util.concurrent.CopyOnWriteArraySet;
2829

@@ -296,6 +297,11 @@ public void destroy() {
296297
if (mCatalystInstance != null) {
297298
mCatalystInstance.destroy();
298299
}
300+
if (ReactFeatureFlags.enableReactContextCleanupFix) {
301+
mLifecycleEventListeners.clear();
302+
mActivityEventListeners.clear();
303+
mWindowFocusEventListeners.clear();
304+
}
299305
}
300306

301307
/** Should be called by the hosting Fragment in {@link Fragment#onActivityResult} */

ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java

+3
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ public class ReactFeatureFlags {
6262

6363
/** Enables Static ViewConfig in RN Android native code. */
6464
public static boolean enableExperimentalStaticViewConfigs = false;
65+
66+
/** Enables a more aggressive cleanup during destruction of ReactContext */
67+
public static boolean enableReactContextCleanupFix = false;
6568
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ public void onCatalystInstanceDestroy() {
247247
mEventDispatcher.onCatalystInstanceDestroyed();
248248
mUIImplementation.onCatalystInstanceDestroyed();
249249

250-
getReactApplicationContext().removeLifecycleEventListener(this);
251-
getReactApplicationContext().unregisterComponentCallbacks(mMemoryTrimCallback);
250+
ReactApplicationContext reactApplicationContext = getReactApplicationContext();
251+
if (ReactFeatureFlags.enableReactContextCleanupFix) {
252+
reactApplicationContext.removeLifecycleEventListener(this);
253+
}
254+
reactApplicationContext.unregisterComponentCallbacks(mMemoryTrimCallback);
252255
YogaNodePool.get().clear();
253256
ViewManagerPropertyUpdater.clear();
254257
}

0 commit comments

Comments
 (0)