Skip to content

Commit

Permalink
Call invalidate on ViewManager on context destroy (#37481)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
javache authored and facebook-github-bot committed May 19, 2023
1 parent f10dd3f commit c5e7cd4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ public void onCatalystInstanceDestroy() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ public void onHostDestroy() {}

public void onCatalystInstanceDestroyed() {
mViewOperationsEnabled = false;
mViewManagers.invalidate();
}

public void setViewHierarchyUpdateDebugListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,28 @@ public void onSurfaceStopped(final int surfaceId) {
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()) {
Expand Down

0 comments on commit c5e7cd4

Please sign in to comment.