10.ActivityThread
11.WindowManager和WindowManagerImpl和WindowManagerGlobal
12.ViewRootImpl中通过addToDisplay添加DecorView并显示出来
13.view.assignParent设置DecorView的parent为ViewRootImpl,这也就是View在调用requestLayout方法时最终会调用ViewRootImpl的requestLayout方法的原因
从setContentView流程分析中我们可以看到DecorView是如何创建的,那么DecorView是如何添加到窗口中让我们可以看到的呢?
我们可以查看startAcitivty做了些什么?
Acitivity#startActivity(Intent intent)
- 会直接调用
startActivity(Intent intent, @Nullable Bundle options)
this.startActivity(intent, null);
- 查看
startActivity(Intent intent, @Nullable Bundle options)
,最终会调用startActivityForResult(@RequiresPermission Intent intent, int requestCode,@Nullable Bundle options)
方法
startActivityForResult(intent, -1);
startActivityForResult(intent, requestCode, null);
Activity#startActivityForResult
- 调用Instrumentation的execStartActivity方法得到ActivityResult
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
- 如果ActivityResult不为null,则调用AcitivtyThread的sendActivityResult方法
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
Instrumentation#execStartActivity
- 调用ActivityManagerProxy的startActivity方法,内部会调用远程服务的IBinder接口的transact方法
int result = ActivityManagerNative.getDefault()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);
- 调用Instrumentation的checkStartActivityResult方法检查Activity是否能被启动
checkStartActivityResult(result, intent);
AcitivtyThread#sendActivityResult
调用ApplicationThread的scheduleSendResult方法
mAppThread.scheduleSendResult(token, list);
ApplicationThread#scheduleSendResult
sendMessage(H.SEND_RESULT, res);
AcitivtyThread#sendMessage
- 会直接调用sendMessage(int,Object,int,int,bolean)
sendMessage(what, obj, 0, 0, false);
- 查看sendMessage(int,Object,int,int,bolean),
Message msg = Message.obtain();
...
mH.sendMessage(msg);
H&sendMessage
H是Handler的子类,所以我们可以去查看H的handleMessage方法,前面startActivity的时候在ApplicationThread&scheduleSendResult
时发送了一个msg.what = H.SEND_RESULT
的Message:
public void handleMessage(Message msg) {
switch (msg.what) {
...
case SEND_RESULT:
handleSendResult((ResultData)msg.obj);
break;
...
AcitivtyThread#handleResumeActivity
...
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
...
a.mWindowAdded = true;
wm.addView(decor, l);
可以看到通过WindowManager最终将DecorView添加到相应的Window上去