Activity添加window过程

263 阅读2分钟

「这是我参与2022首次更文挑战的第22天,活动详情查看:2022首次更文挑战」。

Activity的window

之前说了window的工作过程,但是除了我们自己添加window,一些常见的含视图的组件(activity)或者弹窗(Dialog,Toast)也会在创建时添加window,接下来主要说说这几种情况下window的添加情况,首先看Activity的window添加过程。 之前说过Activity的创建过程,在这篇文章最后说过attach过程会与view的创建有关,这里先看attach方法

final void attach(Context context, ActivityThread aThread,
        Instrumentation instr, IBinder token, int ident,
        Application application, Intent intent, ActivityInfo info,
        CharSequence title, Activity parent, String id,
        NonConfigurationInstances lastNonConfigurationInstances,
        Configuration config, String referrer, IVoiceInteractor voiceInteractor,
        Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
    attachBaseContext(context);

    mFragments.attachHost(null /*parent*/);

    mWindow = new PhoneWindow(this, window, activityConfigCallback);
    mWindow.setWindowControllerCallback(mWindowControllerCallback);
    mWindow.setCallback(this);
    mWindow.setOnWindowDismissedCallback(this);
    mWindow.getLayoutInflater().setPrivateFactory(this);

...

由于代码过长,这里省略了一部分,通过以上代码可以得出结论,activity的window最终实例化为phoneWindow对象,同时注意到有这一行代码

mWindow.setCallback(this);

window对activity进行了代理,当window有对应事件产生时activity可以进行相应的处理。有了window之后,之后的view的添加可以猜测也是通过该window进行添加的。为了验证这一点回到activity的第一个生命周期onCreate,在介绍Activity的创建过程最后说过onCreate是在attach后回调的一个方法,通常我们写一个应用时MainActivity的onCreate方法就是我们第一次写代码的地方

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

由于setContentView方法来自父类AppCompatActivity

@Override
public void setContentView(@LayoutRes int layoutResID) {
    getDelegate().setContentView(layoutResID);
}

父类同时是使用了代理,真正的实现在AppCompatDelegateImpl

@Override
public void setContentView(int resId) {
    ensureSubDecor();
    ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content);
    contentParent.removeAllViews();
    LayoutInflater.from(mContext).inflate(resId, contentParent);
    mAppCompatWindowCallback.getWrapped().onContentChanged();
}

发现有些不对,这里并没有见到window的影子,但是这几行代码已经将view的set逻辑体现出来了,首先通过ensureSubDecor方法确保SubDecorView已经生成并且安装到window上,ensureSubDecor方法如下

private void ensureSubDecor() {
    if (!mSubDecorInstalled) {
        mSubDecor = createSubDecor();

        // If a title was set before we installed the decor, propagate it now
        CharSequence title = getTitle();
        if (!TextUtils.isEmpty(title)) {
            if (mDecorContentParent != null) {
                mDecorContentParent.setWindowTitle(title);
            } else if (peekSupportActionBar() != null) {
                peekSupportActionBar().setWindowTitle(title);
            } else if (mTitleView != null) {
                mTitleView.setText(title);
            }
        }

        applyFixedSizeWindow();

        onSubDecorInstalled(mSubDecor);

        mSubDecorInstalled = true;

        ...
    }
}

之前在view的工作原理说过window会包含一个顶级view,也就是DecorView,这个view是一个FrameLayout结构,分为标题栏和内容栏,在这里我们是往内容栏中 Inflate,过程是先通过内容栏的id即android.R.id.content拿到viewGroup,接着往这个viewGroup中引入我们写好的xml布局,并最终回调activity的onContentChanged方法