以下代碼來自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)用了圣猎,方便我們在源碼的海洋中不至于迷失逝钥。
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();
}
而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)用得到的。
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是什么矾飞。
ViewRootImpl繼承了ViewParent,然后我們來看下ViewParent家族一膨。
這里顯示他的另一個子類是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
多么的簡單明了啊啸臀,直接把自己賦值給了子View。所以在這里我們可以直接下結(jié)論了烁落,子View.mParent是他的父View(廢話乘粒,從名字就能看出來了好嗎)。
總結(jié)
- DecorView的mParent為ViewRootImpl
- 普通View的mParent為它的父View
每分析一次源碼都會使自己更接近真相伤塌,希望大家也能自己一步步去跟一遍灯萍。 下一篇打算分析哪個模塊的代碼,暫時還沒有想好每聪,大家如果有好的建議的話旦棉,可以給我留言。好了药薯,拜了個拜绑洛。大吉大利,晚上吃雞M尽U矬浴!巾陕!