上一篇介绍了apk启动的流程到了创建activity的创建这里接着继续分析,先上图片
看高清图请下载
这里从handleLaunchActivity方法继续分析,如不明白的可以参考上一篇的分析android启动流程分析,这边直接就上代码了
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
......
Activity a = performLaunchActivity(r, customIntent);//创建activity
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
reportSizeConfigurations(r);
Bundle oldState = r.state;
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
......
} else {
......
}
}
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
......
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);//通过Instrumentation这个类创建Activity
......
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
......
activity.attach(appContext, this, getInstrumentation(), r.token,//attach方法
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window);
......
int theme = r.activityInfo.getThemeResource();//设置主题
if (theme != 0) {
activity.setTheme(theme);
}
......
if (r.isPersistable()) {//根据本地是否持久化来判断走那个方法,(这个持久化个人理解是activity被不正常销毁造成的)
//这个方法调用的是activity的oncreate方法
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
......
if (!r.activity.mFinished) {
activity.performStart();//调用activity的onstart方法
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) {//r.state是AMS传过来的,也就是说OnRestoreInstanceState的调用取决于AMS是否想恢复activity。
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;
}
通过上面的代码可以看到activity再启动的时候首选调用attach--->oncreate--->onStart--->onPostCreate
对于attach是activity的一些初始化配置,在这个方法中会创建PhoneWindow对象,对于onPostCreate方法是Activity完全起来之后调用的方法(官方解释Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called),对于这个方法目前不知道有什么特殊的用法如有特殊用法,希望能提出。其他的两个方法这里就不多说了我们常用的方法。上面还有个不是生命周期的一部分的是onRestoreInstanceState这个方法的调用是在AMS判定需要调用的时候才会调用(转屏,activity不正常被系统回收等)
final void handleResumeActivity(IBinder token,
boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
......
// TODO Push resumeArgs into the activity for consideration
r = performResumeActivity(token, clearHide, reason);
.....
if (a.mVisibleFromClient && !a.mWindowAdded) {//将decor添加到Window上
a.mWindowAdded = true;
wm.addView(decor, l);
}
....
}
public final ActivityClientRecord performResumeActivity(IBinder token,
boolean clearHide, String reason) {
......
try {
r.activity.onStateNotSaved();
r.activity.mFragments.noteStateNotSaved();
if (r.pendingIntents != null) {
deliverNewIntents(r, r.pendingIntents);
r.pendingIntents = null;
}
if (r.pendingResults != null) {
deliverResults(r, r.pendingResults);
r.pendingResults = null;
}
r.activity.performResume();
......
}
return r;
}
final void performResume() {
performRestart();
.....
mInstrumentation.callActivityOnResume(this);//调用了onResume方法
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onResume()");
}
......
mFragments.dispatchResume();
mFragments.execPendingActions();
onPostResume();//
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onPostResume()");
}
}
final void performRestart() {
mFragments.noteStateNotSaved();
if (mToken != null && mParent == null) {
// No need to check mStopped, the roots will check if they were actually stopped.
WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
}
if (mStopped) {//当时从stop状态恢复的时候走下面的代码
mStopped = false;
synchronized (mManagedCursors) {
final int N = mManagedCursors.size();
for (int i=0; i<N; i++) {
ManagedCursor mc = mManagedCursors.get(i);
if (mc.mReleased || mc.mUpdated) {
if (!mc.mCursor.requery()) {
if (getApplicationInfo().targetSdkVersion
>= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
throw new IllegalStateException(
"trying to requery an already closed cursor "
+ mc.mCursor);
}
}
mc.mReleased = false;
mc.mUpdated = false;
}
}
}
mCalled = false;
mInstrumentation.callActivityOnRestart(this);
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onRestart()");
}
performStart();
}
}
通过上面的方法可以找到activity接下来正常的运行声明周期应该是-onResure---->onPostResure,但是如果要是从stop状态下回复Activity的时候就会先调用onRestart--->onstart-->onResure--->onPostResure
到这里activity就算启动完成了,显现出来了,上面的代码分析是Activity启动的声明周期,下一篇分析Activity关闭的生命周期