1、performLaunchActivity( )
ActivityThread 中的performLaunchActivity ( )在通过classLoader加载到MainActivity之后,反射出一个 MainActivity对象activity,然后会调用activity.attach( ),在这个函数中,会创建window
//mWindow是Activity的一个成员
private Window mWindow;
//可以看出window的最终实现是PhoneWindow
mWindow = new PhoneWindow(this, window);
2、那么 window是如何与activity的视图产生关系的呢?可以看一下activity的setContentView( )函数
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
3、PhoneWindow的setContentView()如下
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
mContentParentExplicitlySet = true;
}
他主要是先创建DecorView,然后把setContentView()中传入的布局添加到mContentParent (R.id.content对应的布局,其中DecorView是一个frameLayout,包含标题栏以及内容视图,其中mContentParent就是这个内容视图)中.
4、视图显示时机
但是目前为止,decorView还没有显示出来,它需要被添加到windowManager中,ActivityThread执行handleResumeActivity( )的时候,先调用Activity的onResume( ),再调用Activity的makeVisible( )函数,此时页面才真正滴显示出来
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}