關(guān)于View中mParent的來龍去脈

以下代碼來自android-26

mParent賦值

View#assignParent

void assignParent(ViewParent parent) {
        if (mParent == null) {
            mParent = parent;
        } else if (parent == null) {
            mParent = null;
        } else {
            throw new RuntimeException("view " + this + " being added, but"
                    + " it already has a parent");
        }
    }

下面的分析我們會分三部分來分析廷没,第一部分是DecorView的由來幸乒,第二部分是DecorView的mParent,而第三部分是普通view(DecorView的子view)的mParent硅急。

首先我們看下View#assignParent都在哪些地方被調(diào)用了圣猎,方便我們在源碼的海洋中不至于迷失逝钥。


image.png

DecorView的由來

首先我們得知道assignParent是在什么地方被調(diào)用的泻仙。其實是在ViewRootImpl#setView中被調(diào)用的糕再。接下來我們一步步分析。我們知道只有startActivity()開啟一個新的activity的時候頁面才會被渲染饰豺。而startActivity會一步步調(diào)用到ActivityThread#performLaunchActivity方法。

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
        // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");

        ActivityInfo aInfo = r.activityInfo;
        if (r.packageInfo == null) {
            r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
                    Context.CONTEXT_INCLUDE_CODE);
        }

        ComponentName component = r.intent.getComponent();
        if (component == null) {
            component = r.intent.resolveActivity(
                mInitialApplication.getPackageManager());
            r.intent.setComponent(component);
        }

        if (r.activityInfo.targetActivity != null) {
            component = new ComponentName(r.activityInfo.packageName,
                    r.activityInfo.targetActivity);
        }

        ContextImpl appContext = createBaseContextForActivity(r);
        Activity activity = null;
        try {
            java.lang.ClassLoader cl = appContext.getClassLoader();
            activity = mInstrumentation.newActivity(
                    cl, component.getClassName(), r.intent);
            StrictMode.incrementExpectedActivityCount(activity.getClass());
            r.intent.setExtrasClassLoader(cl);
            r.intent.prepareToEnterProcess();
            if (r.state != null) {
                r.state.setClassLoader(cl);
            }
        } catch (Exception e) {
            if (!mInstrumentation.onException(activity, e)) {
                throw new RuntimeException(
                    "Unable to instantiate activity " + component
                    + ": " + e.toString(), e);
            }
        }

        try {
            Application app = r.packageInfo.makeApplication(false, mInstrumentation);

            if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
            if (localLOGV) Slog.v(
                    TAG, r + ": app=" + app
                    + ", appName=" + app.getPackageName()
                    + ", pkg=" + r.packageInfo.getPackageName()
                    + ", comp=" + r.intent.getComponent().toShortString()
                    + ", dir=" + r.packageInfo.getAppDir());

            if (activity != null) {
                CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
                Configuration config = new Configuration(mCompatConfiguration);
                if (r.overrideConfig != null) {
                    config.updateFrom(r.overrideConfig);
                }
                if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
                        + r.activityInfo.name + " with config " + config);
                Window window = null;
                if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
                    window = r.mPendingRemoveWindow;
                    r.mPendingRemoveWindow = null;
                    r.mPendingRemoveWindowManager = null;
                }
                appContext.setOuterContext(activity);
                activity.attach(appContext, this, getInstrumentation(), r.token,
                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
                        r.embeddedID, r.lastNonConfigurationInstances, config,
                        r.referrer, r.voiceInteractor, window, r.configCallback);

                if (customIntent != null) {
                    activity.mIntent = customIntent;
                }
                r.lastNonConfigurationInstances = null;
                checkAndBlockForNetworkAccess();
                activity.mStartedActivity = false;
                int theme = r.activityInfo.getThemeResource();
                if (theme != 0) {
                    activity.setTheme(theme);
                }

                activity.mCalled = false;
                //注意下面的代碼 這是activity生命周期開始的地方
                if (r.isPersistable()) {
                    mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
                } else {
                    mInstrumentation.callActivityOnCreate(activity, r.state);
                }
                if (!activity.mCalled) {
                    throw new SuperNotCalledException(
                        "Activity " + r.intent.getComponent().toShortString() +
                        " did not call through to super.onCreate()");
                }
                r.activity = activity;
                r.stopped = true;
                if (!r.activity.mFinished) {
                    activity.performStart();
                    r.stopped = false;
                }
                if (!r.activity.mFinished) {
                    if (r.isPersistable()) {
                        if (r.state != null || r.persistentState != null) {
                            mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
                                    r.persistentState);
                        }
                    } else if (r.state != null) {
                        mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
                    }
                }
                if (!r.activity.mFinished) {
                    activity.mCalled = false;
                    if (r.isPersistable()) {
                        mInstrumentation.callActivityOnPostCreate(activity, r.state,
                                r.persistentState);
                    } else {
                        mInstrumentation.callActivityOnPostCreate(activity, r.state);
                    }
                    if (!activity.mCalled) {
                        throw new SuperNotCalledException(
                            "Activity " + r.intent.getComponent().toShortString() +
                            " did not call through to super.onPostCreate()");
                    }
                }
            }
            r.paused = true;

            mActivities.put(r.token, r);

        } catch (SuperNotCalledException e) {
            throw e;

        } catch (Exception e) {
            if (!mInstrumentation.onException(activity, e)) {
                throw new RuntimeException(
                    "Unable to start activity " + component
                    + ": " + e.toString(), e);
            }
        }

        return activity;
    }

我們看到調(diào)用了mInstrumentation.callActivityOnCreate(activity, r.state);;

Instrumentation#callActivityOnCreate&Activity#performCreate

public void callActivityOnCreate(Activity activity, Bundle icicle) {
        prePerformCreate(activity);
        activity.performCreate(icicle);
        postPerformCreate(activity);
    }

Activity#performCreate

final void performCreate(Bundle icicle) {
        restoreHasCurrentPermissionRequest(icicle);
        onCreate(icicle);
        mActivityTransitionState.readState(icicle);
        performCreateCommon();
    }

我們一般都會在onCreate()方法中調(diào)用setContentView方法

Activity#setContentView

public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }
image.png

而getWindow其實是PhoneWindow允蜈,PhoneWindow其實是屬于內(nèi)核代碼冤吨,不屬于app進(jìn)程,我們進(jìn)去看看饶套。

PhoneWindow#setContentView

@Override
    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) {
            //進(jìn)行了DecorView的初始化
            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;
    }

到這為止我們已經(jīng)知道了DecorView是在PhoneWindow#setContentView被賦值的漩蟆,而PhoneWindow#setContentView又是通過Activity#setContentView一步步調(diào)用得到的。

20160323161350830.png

DecorView.mParent的真相

下面我們換個思路妓蛮,大家如果去看過我的從startActivity一步步到穿越進(jìn)程壁壘就會知道怠李,當(dāng)調(diào)用startActivity的時候會進(jìn)行跨進(jìn)程調(diào)用,最終會調(diào)用到ApplicationThread#scheduleLaunchActivity蛤克,而ApplicationThread是ActivityThread的內(nèi)部類捺癞。

ApplicationThread#scheduleLaunchActivity

public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
         ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
         CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
         int procState, Bundle state, PersistableBundle persistentState,
         List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
         boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {

     updateProcessState(procState, false);

     ActivityClientRecord r = new ActivityClientRecord();

     r.token = token;
     r.ident = ident;
     r.intent = intent;
     r.referrer = referrer;
     r.voiceInteractor = voiceInteractor;
     r.activityInfo = info;
     r.compatInfo = compatInfo;
     r.state = state;
     r.persistentState = persistentState;

     r.pendingResults = pendingResults;
     r.pendingIntents = pendingNewIntents;

     r.startsNotResumed = notResumed;
     r.isForward = isForward;

     r.profilerInfo = profilerInfo;

     r.overrideConfig = overrideConfig;
     updatePendingConfiguration(curConfig);
     //注意這行代碼
     sendMessage(H.LAUNCH_ACTIVITY, r);
 }

可以看到sendMessage會切換到UI線程繼續(xù)進(jìn)行有關(guān)UI的操作,而代碼會進(jìn)入到ActivityThread的內(nèi)部類H extends Handler 构挤。

ActivityThread#H#handleMessage

public void handleMessage(Message msg) {
switch (msg.what) {
                case LAUNCH_ACTIVITY: {
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
                    final ActivityClientRecord r = (ActivityClientRecord) msg.obj;

                    r.packageInfo = getPackageInfoNoCheck(
                            r.activityInfo.applicationInfo, r.compatInfo);
                    handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                } break;
            ……
        }
}

ActivityThread#handleLaunchActivity

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
      ……
        // Initialize before creating the activity
        WindowManagerGlobal.initialize();
        //在上文中我們知道performLaunchActivity最終會的初始化DecorView
        Activity a = performLaunchActivity(r, customIntent);
        ……
        handleResumeActivity(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
        ……
    }

ActivityThread#handleResumeActivity

final void handleResumeActivity(IBinder token,
            boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
          ……
        r = performResumeActivity(token, clearHide, reason);
        ……
        if (r.window == null && !a.mFinished && willBeVisible) {
                r.window = r.activity.getWindow();
                View decor = r.window.getDecorView();
                decor.setVisibility(View.INVISIBLE);
                ViewManager wm = a.getWindowManager();
                WindowManager.LayoutParams l = r.window.getAttributes();
                a.mDecor = decor;
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
                l.softInputMode |= forwardBit;
                if (r.mPreserveWindow) {
                    a.mWindowAdded = true;
                    r.mPreserveWindow = false;
                    // Normally the ViewRoot sets up callbacks with the Activity
                    // in addView->ViewRootImpl#setView. If we are instead reusing
                    // the decor view we have to notify the view root that the
                    // callbacks may have changed.
                    ViewRootImpl impl = decor.getViewRootImpl();
                    if (impl != null) {
                        impl.notifyChildRebuilt();
                    }
                }
                if (a.mVisibleFromClient) {
                    if (!a.mWindowAdded) {
                        a.mWindowAdded = true;
                        //注意這行代碼將DecorView關(guān)聯(lián)到WindowManager髓介,而WindowManager是一個接口,真正的實現(xiàn)是WindowManagerImpl
                        wm.addView(decor, l);
                    } else {
                        // The activity will get a callback for this {@link LayoutParams} change
                        // earlier. However, at that time the decor will not be set (this is set
                        // in this method), so no action will be taken. This call ensures the
                        // callback occurs with the decor set.
                        a.onWindowAttributesChanged(l);
                    }
                }

            // If the window has already been added, but during resume
            // we started another activity, then don't yet make the
            // window visible.
            }
}

通過注釋我們知道WindowManagerImpl會將DecorView
一般每一個handleXXX方法中都會調(diào)用一個對應(yīng)的performXXX方法筋现,內(nèi)部會調(diào)用到到調(diào)用到activity的resume生命周期唐础,我們這里不進(jìn)行探究了。

WindowManagerImpl#addView&WindowManagerGlobal#addView

@Override
    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
        applyDefaultToken(params);
        mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
    }


WindowManagerGlobal#addView

public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow) {
         ……
      public void addView(View view, ViewGroup.LayoutParams params,Display display, Window parentWindow) {
         ViewRootImpl root;
         synchronized (mLock) {
            //我們new了一個ViewRootImpl當(dāng)做root
            root = new ViewRootImpl(view.getContext(), display);
            view.setLayoutParams(wparams);
            mViews.add(view);
            mRoots.add(root);
            mParams.add(wparams);
        try {
            //注意這行代碼
            root.setView(view, wparams, panelParentView);
        } catch (RuntimeException e) {
            // BadTokenException or InvalidDisplayException, clean up.
            synchronized (mLock) {
                final int index = findViewLocked(view, false);
                if (index >= 0) {
                    removeViewLocked(index, true);
                }
            }
            throw e;
        }
      }
   }

首先我們看下ViewRootImpl是什么矾飞。


image.png

ViewRootImpl繼承了ViewParent,然后我們來看下ViewParent家族一膨。


image.png

這里顯示他的另一個子類是ViewGroup。這里我們只要知道就行了洒沦,下面我會說為什么要查看ViewParent家族豹绪。

我們看下ViewRootImpl#setView,代碼比較長申眼,我們只最追重要的看森篷。

ViewRootImpl#setView

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
        synchronized (this) {
            if (mView == null) {
                mView = view;
                //注意這行代碼
                view.assignParent(this);
                ……
            }
        }
    }

通過注釋我們可以看到view.assignParent(this)输钩,而這個View就是我們上文的DecorView。至此仲智,我們終于知道了DecorView.mParent其實就是ViewRootImpl买乃。

子View.mParent的真相

我們費(fèi)了千辛萬苦知道了DecorView.mParent是ViewRootImpl,那么DecorView的直接子view的mParent你說應(yīng)當(dāng)就應(yīng)該是DecorView钓辆,然后一級一級嵌套著剪验。不過這只是我們的猜想,我們還需要驗證前联,怎么驗證功戚,當(dāng)然是再去源碼中查找咯。從上文中我們知道assignParent的調(diào)用鏈出現(xiàn)在了ViewGroup#addViewInner中似嗤。

ViewGroup#addViewInner

image.png
image.png

多么的簡單明了啊啸臀,直接把自己賦值給了子View。所以在這里我們可以直接下結(jié)論了烁落,子View.mParent是他的父View(廢話乘粒,從名字就能看出來了好嗎)。

總結(jié)

  1. DecorView的mParent為ViewRootImpl
  2. 普通View的mParent為它的父View

每分析一次源碼都會使自己更接近真相伤塌,希望大家也能自己一步步去跟一遍灯萍。 下一篇打算分析哪個模塊的代碼,暫時還沒有想好每聪,大家如果有好的建議的話旦棉,可以給我留言。好了药薯,拜了個拜绑洛。大吉大利,晚上吃雞M尽U矬浴!巾陕!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末讨跟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鄙煤,更是在濱河造成了極大的恐慌晾匠,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梯刚,死亡現(xiàn)場離奇詭異凉馆,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進(jìn)店門澜共,熙熙樓的掌柜王于貴愁眉苦臉地迎上來向叉,“玉大人,你說我怎么就攤上這事嗦董∧富眩” “怎么了?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵京革,是天一觀的道長奇唤。 經(jīng)常有香客問我,道長匹摇,這世上最難降的妖魔是什么咬扇? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任,我火速辦了婚禮廊勃,結(jié)果婚禮上懈贺,老公的妹妹穿的比我還像新娘。我一直安慰自己坡垫,他們只是感情好梭灿,可當(dāng)我...
    茶點故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著葛虐,像睡著了一般胎源。 火紅的嫁衣襯著肌膚如雪棉钧。 梳的紋絲不亂的頭發(fā)上屿脐,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天,我揣著相機(jī)與錄音宪卿,去河邊找鬼的诵。 笑死,一個胖子當(dāng)著我的面吹牛佑钾,可吹牛的內(nèi)容都是我干的西疤。 我是一名探鬼主播,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼休溶,長吁一口氣:“原來是場噩夢啊……” “哼代赁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起兽掰,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤芭碍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后孽尽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體窖壕,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了瞻讽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸳吸。...
    茶點故事閱讀 38,809評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖速勇,靈堂內(nèi)的尸體忽然破棺而出晌砾,到底是詐尸還是另有隱情,我是刑警寧澤快集,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布贡羔,位于F島的核電站,受9級特大地震影響个初,放射性物質(zhì)發(fā)生泄漏乖寒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一院溺、第九天 我趴在偏房一處隱蔽的房頂上張望楣嘁。 院中可真熱鬧,春花似錦珍逸、人聲如沸逐虚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叭爱。三九已至,卻和暖如春漱病,著一層夾襖步出監(jiān)牢的瞬間买雾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工杨帽, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留漓穿,地道東北人。 一個月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓注盈,卻偏偏與公主長得像晃危,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子老客,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容