Activity是通过ActivityThead启动的,首先会走handleLaunchActivity方法,之后会走 performLaunchActivity()方法
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
···
Activity a = performLaunchActivity(r, customIntent);
···
}
performLaunchActivity()方法中通过反射创建了Activity的实例,然后会调用其生命周期中的 attach方法,代码如下所示:
performLaunchActivity(){
...
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
···
if (activity != null) {
Context appContext = createBaseContextForActivity(r, activity);
CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Configuration config = new Configuration(mCompatConfiguration);
if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
+ r.activityInfo.name + " with config " + config);
...
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);
···
}
attach方法中,创建了Window的实例PhoneWindow,代码如下:
...
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
mWindow = new PhoneWindow(this, window);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
并且由上面的attach可见,在ActivityThread里会执行Activity的整个生命周期。
之后理所当然地执行了Activity的OnCreate()方法,代码如下:
...
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
...
回调onCreate方法,也意味着调用了一种重要的方法:setContentView()。
setContentView()
进入Activity的setContentView()方法,代码如下:
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
这里getWindow()返回的Window实例就是我们在上述attach方法中创建的PhoneWindow。
PhoneWindow中的setContentView()方法中主要做了两件事:
1.在installDecor()方法中,如果没有DecorView,那么就创建它,如果没有mContentView,那么就创建它,代码如下:
private void installDecor() {
mForceDecorInstall = false;
if (mDecor == null) {
mDecor = generateDecor(-1); //创建DecorView
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
} else {
mDecor.setWindow(this);
}
if (mContentParent == null) {
mContentParent = generateLayout(mDecor); //创建mContentView
// Set up decor part of UI to ignore fitsSystemWindows if appropriate.
mDecor.makeOptionalFitsSystemWindows();
final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(
R.id.decor_content_parent);
...
2.将Activity的布局添加到DecorView的mContentParent中,代码如下:
if (mContentParent == null) {
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
}
执行完Activity的onCreate()方法后,会将DecorView添加到Window中,同时会创建ViewRootImpl对象,并将ViewRootImpl对象和DecorView建立关联,最后开始View的绘制流程,具体流程如下:
在handleResumeActivity()方法中将decorview添加到window,代码如下:
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView(); //获取decorview
decor.setVisibility(View.INVISIBLE);
...
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
wm.addView(decor, l); //添加到window
} 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);
}
}
这里的wm.addView()走的是
WindowManagerImpl中的addView方法,即:
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}
在WindowManagerGlobal中的addView()方法,如下:
...
root = new ViewRootImpl(view.getContext(), display); //创建ViewRootImpl
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// do this last because it fires off messages to start doing things
try {
root.setView(view, wparams, panelParentView); //ViewRootImpl与Decorview建立联系
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
if (index >= 0) {
removeViewLocked(index, true);
}
throw e;
}
}
最后在ViewRootImpl的setView()方法中执行requestLayout()方法,最终进入View的绘制流程。
流程图如下: