Activity啟動過程

Activity啟動流程分為三步:

1. Launcher請求ATMS過程

image.png

2. ATMS到ApplicationThread的調(diào)用過程

image.png

3. ActivityThread啟動Activity過程

image.png

ActivityThread啟動Activity的過程中performLauncherActivity的工作如下:
1.從ActivityClientRecord中獲取待啟動的Activity的組件信息
2.通過Instrumentation的newActivity方法使用類加載器創(chuàng)建Activity
3.通過LoadedApk的makeApplication方法來嘗試創(chuàng)建Applocation對象
4.創(chuàng)建ContextImpl對象并通過Activity的attach方法來完成一些重要的數(shù)據(jù)的初始化
(此處還會創(chuàng)建PhoneWindow對象)
5.調(diào)用Activity的onCreate方法

oncreate前創(chuàng)建了PhoneWindow,PhoneWindow內(nèi)部有個DecorView,
然后在onresume之前,把DecorView通過wm.addview方法給添加進去

  void makeVisible() {
        if (!mWindowAdded) {
            ViewManager wm = getWindowManager();
            wm.addView(mDecor, getWindow().getAttributes());
            mWindowAdded = true;
        }
        mDecor.setVisibility(View.VISIBLE);
    }

此處就涉及了WindowManagerService的知識點;

wm.addview
實際調(diào)用的是WindowManagerImpl.addView

@Override
    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
    //設(shè)置默認token
        applyDefaultToken(params);
        mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow,
                mContext.getUserId());
    }

交給了WindowManagerGolbal

public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow, int userId) {
        //...省略
        ViewRootImpl root;
        View panelParentView = null;

        synchronized (mLock) {
           //...省略
           
          //創(chuàng)建ViewRootImpl
            root = new ViewRootImpl(view.getContext(), display);
            //給DecorView設(shè)置LayoutParams信息
            view.setLayoutParams(wparams);
            //保存DecorView
            mViews.add(view);
            //保存ViewRootImpl
            mRoots.add(root);
            //保存WindowManager.LayoutParams
            mParams.add(wparams);

            // do this last because it fires off messages to start doing things
            try {
                //ViewRootImpl#setView
                root.setView(view, wparams, panelParentView, userId);
            } catch (RuntimeException e) {
                // BadTokenException or InvalidDisplayException, clean up.
                if (index >= 0) {
                    removeViewLocked(index, true);
                }
                throw e;
            }
        }
    }

WindowManagerGolbal中創(chuàng)建了ViewRootImpl,調(diào)用ViewRootImpl的setVIew方法

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView,
            int userId) {
        synchronized (this) {
            if (mView == null) {
                //..省略一堆設(shè)置過程
                // any other events from the system.
                requestLayout();
                //...省略
               try {
                    mOrigWinpatibility(mWindowAttributes);
                    //將窗口添加到WMS上面 這里打個標記留意下
                    res = mWindowSession.addToDisplayAsUser(mWindow, mSeq, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(), userId, mTmpFrame,
                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                            mAttachInfo.mDisplayCutout, inputChannel,
                            mTempInsets, mTempControls);
                    setFrame(mTmpFrame);
                } catch (RemoteException e) {
                    mAdded = false;
                    mView = null;
                    mAttachInfo.mRootView = null;
                    inputChannel = null;
                    mFallbackEventHandler.setView(null);
                    unscheduleTraversals();
                    setAccessibilityFocus(null, null);
                    throw new RuntimeException("Adding window failed", e);
                } finally {
                    if (restore) {
                        attrs.restore();
                    }
                }
                //這里給DecorView設(shè)置mParent
                view.assignParent(this);
                //...省略
                
        }
    }
}

requestLayout就是在繪制window中的DecorView和他的子view,
mWindowSession.addToDisplayAsUser就是把window交給wms,而不是直接把view交給wms;

requestLayout就是view繪制的起點,此處又引出view的繪制相關(guān)概念:
http://www.itdecent.cn/p/a16cb1d807fd

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容