基于4.4 src:
View的onAttachToWindow() 是在其dispatchAttachedToWindow(AttachInfo info, int visibility)里被无条件调用的,
而View的dispatchAttachedToWindow()有两个被调用途径:
1. ViewRootImpl 第一次 performTraversal()时会将整个view tree里所有有view的 dispatchAttachedToWindow() DFS 调用一遍.
2. ViewGroup的 addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout):
AttachInfo ai = mAttachInfo;
if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
.........................................
child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
.........................................
}
AttachInfo 是从ViewRootImpl传下来的,ViewRootImpl构造时:
mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);