Call invalidate on ViewManager on context destroy (#37481)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37481

ViewManagers inherit from BaseJavaModule so we should honour the same lifecycle  for them as we do for native modules, which is to call `invalidate` / `onCatalystInstanceDestroy` when the ReactContext is destroyed. This allows the ViewManager to do any cleanup which cannot happen at the View level.

Changelog: [Android][Fixed] ViewManagers now receive an invalidate callback

Reviewed By: cortinico

Differential Revision: D45945678

fbshipit-source-id: 27c26d951b50a734c42eb033a46e599ef939e29f
This commit is contained in:
Pieter De Baets
2023-12-19 10:17:43 +01:00
committed by fortmarek
parent e70166a3a8
commit 99cdaaacce
3 changed files with 24 additions and 6 deletions
@@ -429,6 +429,7 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
mEventDispatcher.unregisterEventEmitter(FABRIC);
mReactApplicationContext.unregisterComponentCallbacks(mViewManagerRegistry);
mViewManagerRegistry.invalidate();
// Remove lifecycle listeners (onHostResume, onHostPause) since the FabricUIManager is going
// away. Then stop the mDispatchUIFrameCallback false will cause the choreographer
@@ -792,6 +792,7 @@ public class UIImplementation {
public void onCatalystInstanceDestroyed() {
mViewOperationsEnabled = false;
mViewManagers.invalidate();
}
public void setViewHierarchyUpdateDebugListener(
@@ -103,12 +103,28 @@ public final class ViewManagerRegistry implements ComponentCallbacks2 {
viewManagers = new ArrayList<>(mViewManagers.values());
}
Runnable runnable =
new Runnable() {
@Override
public void run() {
for (ViewManager viewManager : viewManagers) {
viewManager.onSurfaceStopped(surfaceId);
}
() -> {
for (ViewManager viewManager : viewManagers) {
viewManager.onSurfaceStopped(surfaceId);
}
};
if (UiThreadUtil.isOnUiThread()) {
runnable.run();
} else {
UiThreadUtil.runOnUiThread(runnable);
}
}
/** Called on instance destroy */
public void invalidate() {
final List<ViewManager> viewManagers;
synchronized (this) {
viewManagers = new ArrayList<>(mViewManagers.values());
}
Runnable runnable =
() -> {
for (ViewManager viewManager : viewManagers) {
viewManager.invalidate();
}
};
if (UiThreadUtil.isOnUiThread()) {