Activity 的Window创建过程

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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值