Activity启动流程分为三步:
1. Launcher请求ATMS过程
2. ATMS到ApplicationThread的调用过程
3. ActivityThread启动Activity过程
ActivityThread启动Activity的过程中performLauncherActivity的工作如下:
1.从ActivityClientRecord中获取待启动的Activity的组件信息
2.通过Instrumentation的newActivity方法使用类加载器创建Activity
3.通过LoadedApk的makeApplication方法来尝试创建Applocation对象
4.创建ContextImpl对象并通过Activity的attach方法来完成一些重要的数据的初始化
(此处还会创建PhoneWindow对象)
5.调用Activity的onCreate方法
oncreate前创建了PhoneWindow,PhoneWindow内部有个DecorView,
然后在onresume之前,把DecorView通过wm.addview方法给添加进去
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
此处就涉及了WindowManagerService的知识点;
wm.addview
实际调用的是WindowManagerImpl.addView
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
//设置默认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) {
//...省略
//创建ViewRootImpl
root = new ViewRootImpl(view.getContext(), display);
//给DecorView设置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中创建了ViewRootImpl,调用ViewRootImpl的setVIew方法
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView,
int userId) {
synchronized (this) {
if (mView == null) {
//..省略一堆设置过程
// any other events from the system.
requestLayout();
//...省略
try {
mOrigWinpatibility(mWindowAttributes);
//将窗口添加到WMS上面 这里打个标记留意下
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设置mParent
view.assignParent(this);
//...省略
}
}
}
requestLayout就是在绘制window中的DecorView和他的子view,
mWindowSession.addToDisplayAsUser就是把window交给wms,而不是直接把view交给wms;
requestLayout就是view绘制的起点,此处又引出view的绘制相关概念:
https://www.jianshu.com/p/a16cb1d807fd