美文网首页
Window添加过程

Window添加过程

作者: 重新起步 | 来源:发表于2023-02-15 22:32 被阅读0次

创建activity的时候,
Activity的performLaunchActivity方法会调用activity的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,
            IBinder shareableActivityToken) {
           。。。省略
           mWindow = new PhoneWindow(this, window, 
           activityConfigCallback);     
mWindow.setWindowControllerCallback(mWindowControllerCallback);
           mWindow.setCallback(this);
           mWindow.setOnWindowDismissedCallback(this);
           mWindow.getLayoutInflater().setPrivateFactory(this);
。。。省略
           mWindowManager = mWindow.getWindowManager();
}

创建了一个PhoneWindow,会在activity的oncreate方法调用setContenetView的时候,PhoneWindow会创建DecorView

private void installDecor() {
        mForceDecorInstall = false;
        if (mDecor == null) {
            mDecor = generateDecor(-1);
            mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            mDecor.setIsRootNamespace(true);
            if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
                mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
            }
        } else {
            mDecor.setWindow(this);
        }

最后ActivityThread会在handleResumeActivity的时候,把DecorView通过WindowManager.addView方法给add上去

 @Override
    public void handleResumeActivity(ActivityClientRecord r, boolean finalStateRequest,
            boolean isForward, String reason) {
  View decor = r.window.getDecorView();
            decor.setVisibility(View.INVISIBLE);
            ViewManager wm = a.getWindowManager();
            WindowManager.LayoutParams l = r.window.getAttributes();
            a.mDecor = decor;
。。。
 if (a.mVisibleFromClient) {
                if (!a.mWindowAdded) {
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
。。。

明天继续分析addView

相关文章

网友评论

      本文标题:Window添加过程

      本文链接:https://www.haomeiwen.com/subject/llmukdtx.html