Remove recycled view from parent by default (#52792)

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

With view recyling enabled only on a subset of the components, a recyclabe view could still be attached to its parent if that parent is not recyclable. This diff removes any recycled view from its parent. This will allow the view to be attached to a new parent on mount.

This diff removes the assert checking for parents still set on views pushed on the recycle stack.

Changelog: [Internal]

Reviewed By: mdvacca, sammy-SC

Differential Revision: D78814943

fbshipit-source-id: bb754ce5f526acbf263f23646335228447278562
This commit is contained in:
Nick Lefever authored and Facebook GitHub Bot committed 2025-07-23 12:16:28 -07:00
1 parent 729e61bfe1
commit e57ae6b964
1 file changed
+11 -12
@@ -9,10 +9,10 @@ package com.facebook.react.uimanager;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
@@ -211,7 +211,16 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
T view = null;
@Nullable Stack<T> recyclableViews = getRecyclableViewStack(reactContext.getSurfaceId(), true);
if (recyclableViews != null && !recyclableViews.empty()) {
view = recycleView(reactContext, recyclableViews.pop());
T recyclableView = recyclableViews.pop();
// When view recycling isn't enabled for all components, a recyclable view could still be
// attached to a non-recyclable view. This guarantees that recycled view have been removed
// from their parent.
if (ReactNativeFeatureFlags.enableViewRecycling() && recyclableView.getParent() != null) {
((ViewGroup) recyclableView.getParent()).removeView(recyclableView);
}
view = recycleView(reactContext, recyclableView);
} else {
view = createViewInstance(reactContext);
}
@@ -260,16 +269,6 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
if (recyclableViews != null) {
T recyclableView = prepareToRecycleView(themedReactContext, view);
if (recyclableView != null) {
Assertions.assertCondition(
recyclableView.getParent() == null,
"Recycled view ["
+ view.getId()
+ "] should not be attached to a parent. View: "
+ view
+ " Parent: "
+ recyclableView.getParent()
+ " ThemedReactContext: "
+ themedReactContext);
recyclableViews.push(recyclableView);
}
}