在 Android Launcher启动应用程序流程源码解析一文中从点击应用程序图标startActivity()
开始,一直讲解到Application
和MainActivity
的onCreate()
方法被调用。中间流程有些高能,高能部分是两篇博文共有的地方,没办法绕开。本文主要讲述从Activity#onCreate()
到Activity#onResume()
的流程。想追本溯源的同学建议先阅读 Android Launcher启动应用程序流程源码解析之后,再回头读这篇博文。不啰嗦了,Here we go~
无论显式还是隐式启动Activity
,都会进入到ActivityThread#handleLaunchActivity()
源码位置:frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread#handleLaunchActivity()
private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
......
// 加载activity,然后调用onCreate,onStart方法
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
// 调用onResume方法
handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed);
...
}
...
}
注释有点剧透,,今天的重点全在这个方法里。首先看下performLaunchActivity()
ActivityThread#performLaunchActivity()
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);
...
}
...
try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
if (activity != null) {
...
activity.attach(...)
...
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
...
if (!r.activity.mFinished) {
activity.performStart();
r.stopped = false;
}
...
return activity;
}
mInstrumentation.callActivityOnCreate
这行代码的作用就是调用Activity#onCreate()
,在 Android Launcher启动应用程序流程源码解析已经介绍过,这里就不再赘述了。直接看activity.performStart()
,这个方法的名字十分吸引人呐~跟进。
源码位置:frameworks/base/core/java/android/app/Activity.java
Activity#performStart()
final void performStart() {
mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
mFragments.noteStateNotSaved();
mCalled = false;
mFragments.execPendingActions();
mInstrumentation.callActivityOnStart(this);
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onStart()");
}
mFragments.dispatchStart();
mFragments.reportLoaderStart();
mActivityTransitionState.enterReady(this);
}
真相已经呼之欲出了~,跟进。
源码位置:frameworks/base/core/java/android/app/Instrumentation.java
Instrumentation#callActivityOnStart()
public void callActivityOnStart(Activity activity) {
activity.onStart();
}
哈哈~又找到一个方法Activity#onStart()
。真是轻而易举~
现在回到ActivityThread#performLaunchActivity()
中的handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed)
,跟进。
源码位置:frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread#handleResumeActivity()
ActivityThread#ActivityClientRecord()
final void handleResumeActivity(IBinder token,
boolean clearHide, boolean isForward, boolean reallyResume) {
...
ActivityClientRecord r = performResumeActivity(token, clearHide);
...
}
public final ActivityClientRecord performResumeActivity(IBinder token,
boolean clearHide) {
...
r.activity.performResume();
...
}
中间都是各种状态效验,没撒好说的。直接跟进。
源码位置:frameworks/base/core/java/android/app/Activity.java
Activity#performResume()
final void performResume() {
performRestart();
...
mInstrumentation.callActivityOnResume(this);
...
}
在这里有两个分支
1. 在执行onResume()
前需要先执行onRestart()
和onStart()
。例如从上个Activity
返回的时候,这里不作为重点,但也会稍微看下。
2. 直接执行onResume()
分支1:
Activity#performRestart()
final void performRestart() {
...
mInstrumentation.callActivityOnRestart(this);
performStart();
}
这两个方法在上文都分析过了,不再赘述。
分支2:
源码位置:frameworks/base/core/java/android/app/Instrumentation.java
Instrumentation#callActivityOnStart()
public void callActivityOnResume(Activity activity) {
activity.mResumed = true;
activity.onResume();
if (mActivityMonitors != null) {
synchronized (mSync) {
final int N = mActivityMonitors.size();
for (int i=0; ifinal ActivityMonitor am = mActivityMonitors.get(i);
am.match(activity, activity, activity.getIntent());
}
}
}
}
直接调用Activity#OnResume()
。
结束了。这篇博客结束的比之前任何一篇博文都要快,都要好理解。这一切的前提是已经阅读过我原来的博文 Android Launcher启动应用程序流程源码解析。
更多Framework源码解析,请移步 Framework源码解析系列[目录]