Activity啟動流程分為三步:
1. Launcher請求ATMS過程
2. ATMS到ApplicationThread的調(diào)用過程
3. ActivityThread啟動Activity過程
ActivityThread啟動Activity的過程中performLauncherActivity的工作如下:
1.從ActivityClientRecord中獲取待啟動的Activity的組件信息
2.通過Instrumentation的newActivity方法使用類加載器創(chuàng)建Activity
3.通過LoadedApk的makeApplication方法來嘗試創(chuàng)建Applocation對象
4.創(chuàng)建ContextImpl對象并通過Activity的attach方法來完成一些重要的數(shù)據(jù)的初始化
(此處還會創(chuàng)建PhoneWindow對象)
5.調(diào)用Activity的onCreate方法
oncreate前創(chuàng)建了PhoneWindow,PhoneWindow內(nèi)部有個(gè)DecorView虑椎,
然后在onresume之前工秩,把DecorView通過wm.addview方法給添加進(jìn)去
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
此處就涉及了WindowManagerService的知識點(diǎn)捏鱼;
wm.addview
實(shí)際調(diào)用的是WindowManagerImpl.addView
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
//設(shè)置默認(rèn)token
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow,
mContext.getUserId());
}
交給了WindowManagerGolbal
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow, int userId) {
//...省略
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
//...省略
//創(chuàng)建ViewRootImpl
root = new ViewRootImpl(view.getContext(), display);
//給DecorView設(shè)置LayoutParams信息
view.setLayoutParams(wparams);
//保存DecorView
mViews.add(view);
//保存ViewRootImpl
mRoots.add(root);
//保存WindowManager.LayoutParams
mParams.add(wparams);
// do this last because it fires off messages to start doing things
try {
//ViewRootImpl#setView
root.setView(view, wparams, panelParentView, userId);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
if (index >= 0) {
removeViewLocked(index, true);
}
throw e;
}
}
}
WindowManagerGolbal中創(chuàng)建了ViewRootImpl,調(diào)用ViewRootImpl的setVIew方法
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView,
int userId) {
synchronized (this) {
if (mView == null) {
//..省略一堆設(shè)置過程
// any other events from the system.
requestLayout();
//...省略
try {
mOrigWinpatibility(mWindowAttributes);
//將窗口添加到WMS上面 這里打個(gè)標(biāo)記留意下
res = mWindowSession.addToDisplayAsUser(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(), userId, mTmpFrame,
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mDisplayCutout, inputChannel,
mTempInsets, mTempControls);
setFrame(mTmpFrame);
} catch (RemoteException e) {
mAdded = false;
mView = null;
mAttachInfo.mRootView = null;
inputChannel = null;
mFallbackEventHandler.setView(null);
unscheduleTraversals();
setAccessibilityFocus(null, null);
throw new RuntimeException("Adding window failed", e);
} finally {
if (restore) {
attrs.restore();
}
}
//這里給DecorView設(shè)置mParent
view.assignParent(this);
//...省略
}
}
}
requestLayout就是在繪制window中的DecorView和他的子view扇丛,
mWindowSession.addToDisplayAsUser就是把window交給wms簸呈,而不是直接把view交給wms啤誊;
requestLayout就是view繪制的起點(diǎn)豪嚎,此處又引出view的繪制相關(guān)概念:
http://www.reibang.com/p/a16cb1d807fd