ActivityThread performLaunchActivity
今天就想单独分析下这个函数,理解这个函数对于我们理解Activity的生命周期还是很有帮助的,这个函数真正执行了Activity的创建,并执行了onCreate函数。
新建Activity对象
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent)
ActivityInfo aInfo = r.activityInfo;
......
ComponentName component = r.intent.getComponent();
......
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.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);
}
}
这部分很好理解,就是使用反射方法,新建了activity对象
attach
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) {
Context appContext = createBaseContextForActivity(r, activity);
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;
}
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);
这段代码一开始就让人感觉有点懵
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
咋一看感觉新建了一个Application对象,其实不然,可以看下makeApplication
函数的实现:
public Application makeApplication(boolean forceDefaultAppClass,
Instrumentation instrumentation) {
if (mApplication != null) {
return mApplication;
}
我只截取了最开始的代码,由于在执行创建Activity逻辑时mApplication对象已经创建,故这段代码会直接返回应用进程创建时新建的Application对象。
再回到attach的逻辑,接下来系统创建了Activity的appContext,获取了Activity的title,最后执行了attach
函数,注意,此时还没有执行Activity的onCreate
函数。看下attach
函数的参数,getInstrumentation
,r.intent
……,可以发现在Activity执行onCreate
函数之前,Activity的属性已经被设置了。
onCreate
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
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()");
}
......
mActivities.put(r.token, r);
在执行onCreate函数之前,首先获取并设置Activity的Theme信息。activity.mCalled
变量标识Activity生命周期的函数是否已经执行,此处用来标识onCreate
是否执行。如果执行完mInstrumentation.callActivityOnCreate
之后发现activity.mCalled
没有被设置为true,则可以认为Activity的onCreate
没有被执行,系统抛出异常。最后将代表Activity的ActivityRecord保存在mActivities的ArrayMap结构中