@@ -289,6 +289,13 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
289289 */
290290 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000 ;
291291
292+ /**
293+ * When set, this ViewGroup will not dispatch onAttachedToWindow calls
294+ * to children when adding new views. This is used to prevent multiple
295+ * onAttached calls when a ViewGroup adds children in its own onAttached method.
296+ */
297+ private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000 ;
298+
292299 /**
293300 * Indicates which types of drawing caches are to be kept in memory.
294301 * This field should be made private, so it is hidden from the SDK.
@@ -2154,8 +2161,12 @@ public void dispatchFinishTemporaryDetach() {
21542161 */
21552162 @ Override
21562163 void dispatchAttachedToWindow (AttachInfo info , int visibility ) {
2164+ mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW ;
21572165 super .dispatchAttachedToWindow (info , visibility );
2166+ mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW ;
2167+
21582168 visibility |= mViewFlags & VISIBILITY_MASK ;
2169+
21592170 final int count = mChildrenCount ;
21602171 final View [] children = mChildren ;
21612172 for (int i = 0 ; i < count ; i ++) {
@@ -3321,7 +3332,7 @@ private void addViewInner(View child, int index, LayoutParams params,
33213332 }
33223333
33233334 AttachInfo ai = mAttachInfo ;
3324- if (ai != null ) {
3335+ if (ai != null && ( mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW ) == 0 ) {
33253336 boolean lastKeepOn = ai .mKeepScreenOn ;
33263337 ai .mKeepScreenOn = false ;
33273338 child .dispatchAttachedToWindow (mAttachInfo , (mViewFlags &VISIBILITY_MASK ));
0 commit comments