Call stopSurface when ReactRootView is unregistered

Summary:
In the past, in Fabric (Android), we never called stopSurface. Ever! This is bad for memory and can cause other issues, so... let's not do that.

Instead, when the ReactRootView is being torn down, we check the View ID to see if it's a Fabric RootView, and if it is, we call Fabric's stopSurface method.

Fabric stopSurface only has impact on (1) Fabric mount instructions being executed after that point and (2) tells JS to stop running the surface, on the JS thread, asynchronously.

Anecdotally it looks like all the MountItems involved in deleting and removing views from the hierarchy are executed before stopSurface is called.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D21965837

fbshipit-source-id: 5169c5a1e339fd9e016ae1234d8389b2bd30be70
This commit is contained in:
Joshua Gross
2020-06-09 23:20:34 -07:00
committed by Facebook GitHub Bot
parent 1c92b1cff6
commit 03174f1cad
4 changed files with 30 additions and 0 deletions
@@ -431,6 +431,23 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@ThreadConfined(UI)
public void unmountReactApplication() {
UiThreadUtil.assertOnUiThread();
// Stop surface in Fabric.
// Calling FabricUIManager.stopSurface causes the C++ Binding.stopSurface
// to be called synchronously over the JNI, which causes an empty tree
// to be committed via the Scheduler, which will cause mounting instructions
// to be queued up and synchronously executed to delete and remove
// all the views in the hierarchy.
if (mReactInstanceManager != null) {
final ReactContext reactApplicationContext = mReactInstanceManager.getCurrentReactContext();
if (reactApplicationContext != null && getUIManagerType() == FABRIC) {
@Nullable
UIManager uiManager =
UIManagerHelper.getUIManager(reactApplicationContext, getUIManagerType());
if (uiManager != null) {
uiManager.stopSurface(this.getId());
}
}
}
if (mReactInstanceManager != null && mIsAttachedToInstance) {
mReactInstanceManager.detachRootView(this);