diff options
| author | Riddle Hsu <riddlehsu@google.com> | 2018-06-08 15:54:08 +0800 |
|---|---|---|
| committer | Riddle Hsu <riddlehsu@google.com> | 2018-06-20 05:09:32 +0000 |
| commit | 95459d3e6e5b057c68bb59f04500f4b2fea81cc1 (patch) | |
| tree | 7c59584c191903d0be609f3a12d0794d8b73e9a9 /core/java/android | |
| parent | acda839b9e3fa12d0f0b40ffd8ec3708ad0a6038 (diff) | |
Ensure consistent attach/detach callback on transient view
- Not allow to add as transient view if it still has a parent.
(The normal usage of addTransientView should be after removeView)
- If a view container is not attached, its transient view should
not be attached.
- If a transient view is already detached from parent, no need
to detach again when removing the transient view.
Bug: 109814657
Test: atest FrameworksCoreTests:ViewGroupTransientViewTest
Change-Id: I243cc292e4aaba987d48eeb202559de866a8c208
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/view/ViewGroup.java | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java index 794431966443..d0539ae30719 100644 --- a/core/java/android/view/ViewGroup.java +++ b/core/java/android/view/ViewGroup.java @@ -4652,7 +4652,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager * which is added in order to fade it out in its old location should be removed * once the animation is complete.</p> * - * @param view The view to be added + * @param view The view to be added. The view must not have a parent. * @param index The index at which this view should be drawn, must be >= 0. * This value is relative to the {@link #getChildAt(int) index} values in the normal * child list of this container, where any transient view at a particular index will @@ -4661,9 +4661,14 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager * @hide */ public void addTransientView(View view, int index) { - if (index < 0) { + if (index < 0 || view == null) { return; } + if (view.mParent != null) { + throw new IllegalStateException("The specified view already has a parent " + + view.mParent); + } + if (mTransientIndices == null) { mTransientIndices = new ArrayList<Integer>(); mTransientViews = new ArrayList<View>(); @@ -4683,7 +4688,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager mTransientViews.add(view); } view.mParent = this; - view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); + if (mAttachInfo != null) { + view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags & VISIBILITY_MASK)); + } invalidate(true); } @@ -4705,7 +4712,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager mTransientViews.remove(i); mTransientIndices.remove(i); view.mParent = null; - view.dispatchDetachedFromWindow(); + if (view.mAttachInfo != null) { + view.dispatchDetachedFromWindow(); + } invalidate(true); return; } |
