文字描述如下
1 launcher 通過ServiceManager找到AMS(AMS所在進(jìn)程為SystemServer)通知AMS啟動對應(yīng)Activity
2 AMS收到通知,通知launcher onPause,完畢后AMS準(zhǔn)備啟動Activity
3 Activity查看對應(yīng)activity所在進(jìn)程是否存在,不存在則通知zygote fork 進(jìn)程
4 完畢后利用反射啟動ActivityThread,ActivityThread啟動后會將appThead attch到AMS
5 AMS然后會將appThread與先前fork的進(jìn)程綁定,然后調(diào)用AppThread的bindApplication
6 AppThread會利用Hanlder發(fā)送bindApplicatoin消息
7 然后調(diào)用handleBindApplication,makeApplication 再次利用反射,創(chuàng)建Applicatoin類
8 至此Application啟動完成
9 然后AMS會繼承調(diào)用AppThread通知ActivityThread啟動Activity
10 Activity收到通知,HanldeLanunActivity->performLaunchActivity->activity.attach()
11 然后會創(chuàng)建PhoneWindow,調(diào)用callActivityOnCreate ->setContentView->installDecorView
12 然后到HanldeResumeActivity->WindowManager.addView()->WindowManagerImpl.addView()->WindowManagerImpl.addView()
13 新建ViewRootImpl(view.getContext(), display),然后setView()->performTraversals
14 performTraversal會判斷是否存在Surface,如果沒有則進(jìn)行relayoutWindow->此時會創(chuàng)建Surface用于繪制 -> 走至少一次的performMeasure(至多兩次,第一次為子類希望的大小,如果不合理(父布局判斷)則重新測量,父布局提供具體限制)->再次判斷是否存在Surface,(此時存在)->進(jìn)行onDraw()
private void performTraversals() {
// 省略無關(guān)代碼
// Execute enqueued actions on every traversal in case a detached view enqueued an action
getRunQueue().executeActions(mAttachInfo.mHandler);
// 省略無關(guān)代碼
boolean hwInitialized = false;
boolean contentInsetsChanged = false;
boolean newSurface = false;
/**
hadSurface 第一次進(jìn)入,mSurface.isValid 為false
因?yàn)檫€沒有創(chuàng)建Surface
再次進(jìn)入已經(jīng)有Surface了,所以重新為true
**/
boolean hadSurface = mSurface.isValid();
// 省略無關(guān)代碼
// 此時創(chuàng)建Surface,利用WindowSession代理對象,WindowManagerService通信,調(diào)用其relayoutWindow函數(shù)
// 會創(chuàng)建SurfaceContronll然后獲取Surface
relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
// 省略無關(guān)代碼
if (!hadSurface) {//hadSurface在第一次進(jìn)入時草冈,為false.
if (mSurface.isValid()) {//此時Surface已經(jīng)存在,因?yàn)樽哌^了一遍relayoutWindow
// If we are creating a new surface, then we need to
// completely redraw it. Also, when we get to the
// point of drawing it we will hold off and schedule
// a new traversal instead. This is so we can tell the
// window manager about all of the windows being displayed
// before actually drawing them, so it can display then
// all at once.
newSurface = true;//標(biāo)記新建Surface
}
}
}
// Ask host how big it wants to be
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
if (measureAgain) {
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
}
if (didLayout) {
performLayout(lp, mWidth, mHeight);
}
// 省略無關(guān)代碼
boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible;
if (!cancelDraw && !newSurface) {//第一次進(jìn)來,newSurface為ture,所以走else分支意鲸。
performDraw();
} else {
if (isViewVisible) {
// Try again
scheduleTraversals();//重新Travesals
}
}
如圖所示
另外是此圖給了我靈感,表示感謝