diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java index d5fafeb70fc82c..2015936aab549d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java @@ -666,6 +666,15 @@ public synchronized void removeRootView(int rootViewTag) { mRootTags.delete(rootViewTag); } + /** + * Return root view num + * + * @return The num of root view + */ + public synchronized int getRootViewNum() { + return mRootTags.size(); + } + /** * Returns true on success, false on failure. If successful, after calling, output buffer will be * {x, y, width, height}. diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java index 55939a9679dde9..b6cca3dee09ab7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java @@ -170,6 +170,15 @@ public void removeRootView(int rootViewTag) { mOperationsQueue.enqueueRemoveRootView(rootViewTag); } + /** + * Return root view num + * + * @return The num of root view + */ + private int getRootViewNum() { + return mOperationsQueue.getNativeViewHierarchyManager().getRootViewNum(); + } + /** Unregisters a root node with a given tag from the shadow node registry */ public void removeRootShadowNode(int rootViewTag) { synchronized (uiImplementationThreadLock) { @@ -599,6 +608,12 @@ public void measureLayoutRelativeToParent( /** Invoked at the end of the transaction to commit any updates to the node hierarchy. */ public void dispatchViewUpdates(int batchId) { + if (getRootViewNum() <= 0) { + // If there are no RootViews registered, there will be no View updates to dispatch. + // This is a hack to prevent this from being called when Fabric is used everywhere. + // This should no longer be necessary in Bridgeless Mode. + return; + } SystraceMessage.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "UIImplementation.dispatchViewUpdates") .arg("batchId", batchId) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 752586990b6ddb..c4635ffcad73a0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -113,7 +113,6 @@ public interface CustomEventNamesResolver { private volatile int mViewManagerConstantsCacheSize; private int mBatchId = 0; - private int mNumRootViews = 0; public UIManagerModule( ReactApplicationContext reactContext, @@ -403,7 +402,6 @@ public int addRootView( -1); mUIImplementation.registerRootView(rootView, tag, themedRootContext); - mNumRootViews++; Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); return tag; } @@ -427,7 +425,6 @@ public void stopSurface(final int surfaceId) { @ReactMethod public void removeRootView(int rootViewTag) { mUIImplementation.removeRootView(rootViewTag); - mNumRootViews--; } public void updateNodeSize(int nodeViewTag, int newWidth, int newHeight) { @@ -768,12 +765,7 @@ public void onBatchComplete() { listener.willDispatchViewUpdates(this); } try { - // If there are no RootViews registered, there will be no View updates to dispatch. - // This is a hack to prevent this from being called when Fabric is used everywhere. - // This should no longer be necessary in Bridgeless Mode. - if (mNumRootViews > 0) { - mUIImplementation.dispatchViewUpdates(batchId); - } + mUIImplementation.dispatchViewUpdates(batchId); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); }