在scheduleLaunchActivity方法中,首先创建了一个ActivityClientRecord
对象,并对其进行赋值。
ActivityClientRecord:用于记录与Activity相关的数据。
然后通过Handler,将线程由Binder线程
切换到主线程
。最终调用到ActivityThread的handleLaunchActivity
方法。
public final class ActivityThread {
// 省略部分代码
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
// 省略部分代码
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
// 省略部分代码
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
if (!r.activity.mFinished && r.startsNotResumed) {
// The activity manager actually wants this one to start out paused, because it
// needs to be visible but isn’t in the foreground. We accomplish this by going
// through the normal startup (because activities expect to go through onResume()
// the first time they run, before their window is displayed), and then pausing it.
// However, in this case we do -not- need to do the full pause cycle (of freezing
// and such) because the activity manager assumes it can just retain the current
// state it has.
performPauseActivityIfNeeded(r, reason);
// 省略部分代码
}
}
}
}
这里,我们暂时不管performLaunchActivity
方法中做了什么,仅分析后续代码。后续代码中,调用了handleResumeActivity
,猜测它应该会调用Activity的onResume
方法。
根据Activity生命周期推测到:
在
performLaunchActivity
方法里,一定会依次调用Activity的onCreate
、onStart
方法。
带着这个思路,开始分析performLaunchActivity方法。
public final class ActivityThread {
// 省略部分代码
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// 省略部分代码
Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
// 省略部分代码
} catch (Exception e) { /* 省略部分代码 */ }
try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
// 省略部分代码
if (activity != null) {
// 省略部分代码
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.pare