-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Fix handling removal of transitioning views #47634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0c310cb
ca9a22d
00e0e52
150c15e
48b90ad
6e15352
4d6b796
93b0a35
c3da8d0
09b7c62
eb4e4fd
c14e026
a7c7af3
f54164d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,9 @@ public void shutdown() { | |
| private @Nullable ViewGroupDrawingOrderHelper mDrawingOrderHelper; | ||
| private float mBackfaceOpacity; | ||
| private String mBackfaceVisibility; | ||
| private boolean mIsTransitioning = false; | ||
| private @Nullable Set<Integer> mChildrenRemovedWhileTransitioning = null; | ||
|
||
|
|
||
|
|
||
| /** | ||
| * Creates a new `ReactViewGroup` instance. | ||
|
|
@@ -360,12 +363,14 @@ public void setRemoveClippedSubviews(boolean removeClippedSubviews) { | |
| mAllChildren[i] = child; | ||
| child.addOnLayoutChangeListener(mChildrenLayoutChangeListener); | ||
| } | ||
| mChildrenRemovedWhileTransitioning = new HashSet<>(); | ||
kkafar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| updateClippingRect(); | ||
| } else { | ||
| // Add all clipped views back, deallocate additional arrays, remove layoutChangeListener | ||
| Assertions.assertNotNull(mClippingRect); | ||
| Assertions.assertNotNull(mAllChildren); | ||
| Assertions.assertNotNull(mChildrenLayoutChangeListener); | ||
| Assertions.assertNotNull(mChildrenRemovedWhileTransitioning); | ||
kkafar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (int i = 0; i < mAllChildrenCount; i++) { | ||
| mAllChildren[i].removeOnLayoutChangeListener(mChildrenLayoutChangeListener); | ||
| } | ||
|
|
@@ -375,6 +380,7 @@ public void setRemoveClippedSubviews(boolean removeClippedSubviews) { | |
| mClippingRect = null; | ||
| mAllChildrenCount = 0; | ||
| mChildrenLayoutChangeListener = null; | ||
| mChildrenRemovedWhileTransitioning = null; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -401,6 +407,23 @@ public void updateClippingRect() { | |
| updateClippingToRect(mClippingRect); | ||
| } | ||
|
|
||
| @Override | ||
| public void startViewTransition(View view) { | ||
| super.startViewTransition(view); | ||
| mIsTransitioning = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void endViewTransition(View view) { | ||
| super.endViewTransition(view); | ||
| mIsTransitioning = false; | ||
| if (mChildrenRemovedWhileTransitioning != null) { | ||
| mChildrenRemovedWhileTransitioning.clear(); | ||
kkafar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mChildrenRemovedWhileTransitioning = null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private void updateClippingToRect(Rect clippingRect) { | ||
| Assertions.assertNotNull(mAllChildren); | ||
| int clippedSoFar = 0; | ||
|
|
@@ -547,6 +570,11 @@ public void onViewRemoved(View child) { | |
| } else { | ||
| setChildrenDrawingOrderEnabled(false); | ||
| } | ||
|
|
||
| if (mIsTransitioning && mChildrenRemovedWhileTransitioning != null) { | ||
| mChildrenRemovedWhileTransitioning.add(child.getId()); | ||
| } | ||
|
|
||
| super.onViewRemoved(child); | ||
| } | ||
|
|
||
|
|
@@ -661,11 +689,13 @@ public void run() { | |
|
|
||
| /*package*/ void removeViewWithSubviewClippingEnabled(View view) { | ||
| UiThreadUtil.assertOnUiThread(); | ||
|
|
||
| Assertions.assertCondition(mRemoveClippedSubviews); | ||
| Assertions.assertNotNull(mClippingRect); | ||
| Assertions.assertNotNull(mChildrenRemovedWhileTransitioning); | ||
|
|
||
| View[] childArray = Assertions.assertNotNull(mAllChildren); | ||
| view.removeOnLayoutChangeListener(mChildrenLayoutChangeListener); | ||
|
|
||
| int index = indexOfChildInAllChildren(view); | ||
| if (!isViewClipped(childArray[index])) { | ||
| int clippedSoFar = 0; | ||
|
|
@@ -677,6 +707,7 @@ public void run() { | |
| removeViewsInLayout(index - clippedSoFar, 1); | ||
| } | ||
| removeFromArray(index); | ||
|
|
||
| } | ||
|
|
||
| /*package*/ void removeAllViewsWithSubviewClippingEnabled() { | ||
|
|
@@ -693,7 +724,7 @@ public void run() { | |
| * @return {@code true} if the view has been removed from the ViewGroup. | ||
| */ | ||
| private boolean isViewClipped(View view) { | ||
| return view.getParent() == null; | ||
| return view.getParent() == null || (mChildrenRemovedWhileTransitioning != null && mChildrenRemovedWhileTransitioning.contains(view.getId())); | ||
| } | ||
|
|
||
| private int indexOfChildInAllChildren(View child) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is really interesting, and may explain a crash we're seeing. I wonder why we were previously removing this subview twice - potentially messing up some state in ReactViewGroup.
What's your reasoning for removing this?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javache thanks for review. My logic was as follows:
before 0b22b95 you relied on
handleRemoveViewbeing called to perform side effects regarding child drawing order. Implementation ofReactViewgroup.removeViewtook care of handling these sideeffects and detaching child view from parent.On the other hand
ReactViewGroup.removeViewWithSubviewClippingEnabledhad also two responsibilities:mAllChildrenarray, which also retained children already detached due to clipping.Notice, that it had not performed
handleRemoveView.My theory is that in clipping view manager you had call to
parent.removeView(child)to ensure sideeffects ofhandleRemoveViewwere performed and then you calledparent.removeViewWithSubviewClippingEnabled(child)to remove child frommAllChildrenarray, relying on the guard from (1), which prevented Android crash potentially caused by removing twice the same child view.After 0b22b95 side effects of
handleRemoveViewhave been moved toonViewRemovedwhich is called on any attempt of child removal by the Android framework. Therefore we can remove the call toparent.removeViewas all required side effects will be performed by the call toparent.removeViewWithSubviewClippingEnabled(child)- drawing order code, removal frommAllChildrenand child detachment. I've left the check for nullish child, as I couldn't determine what was the logic behind it.