文章目录
- 根Activity启动的整理流程:
-
- 步骤1 Launcher请求AMS
- 步骤2 AMS到ApplicationThread
-
-
- 2.1 AMS.startActivityAsUser():
- 2.2 ActivityStarter.startActivityMayWait():
- 2.3 ActivityStarter.startActivityUnchecked():
- 2.4 StackSupervisor.resumeFocusedStackTopActivityLocked():
- 2.5 ActivityStack.resumeTopActivityUncheckedLocked():
- 2.6 StackSupervisor.startSpecificActivityLocked():
- 2.7 StackSupervisor.realStartActivityLocked():
-
- 步骤3 ApplicationThread通知ActivityThread启动Activity
Activity的启动分为根activity启动和普通activity启动,根activity的启动过程包括了普通activity的启动过程,本文只介绍根activity的启动;
根Activity启动的整理流程:
- Launcher进程请求SystemServer进程的AMS
- AMS请求用户进程的ApplicationThread
- ApplicationThread请求ActivityThread,ActivityThread启动Activity
步骤1 Launcher请求AMS
在Launcher桌面点击应用图标会调用Launcher的startActivitySafely()
1.1 Launcher.startActivitySafely()
boolean startActivitySafely(View v, Intent intent, Object tag) {
boolean success = false;
try {
success = startActivity(v, intent, tag);
}
return success;
}
boolean startActivity(View v, Intent intent, Object tag) {
// 分析1
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 分析2
startActivity(intent, opts.toBundle());
}
- 通过intent设置New Task的Flag,开启一个新的活动栈
- 调用startActivity(),这个方法在Activity类中实现;
1.2 Activity.startActivityForResult():
startActivity()会调用startActivityForResult()
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
}
}
在Activity类中,将启动Activity的请求交给了Instrumentation类
1.3 Instrumentation.execStartActivity():
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
int result = ActivityManager.getService()
.startActivityAsUser(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, resultWho,
requestCode, 0, null, options, user.getIdentifier());
}
通过 ActivityManager.getService()拿到iActivityManager对象,iActivityManager是AMS在用户进程的代理,所以真实调用的其实是AMS的startActivityAsUser();
至此Launcher到AMS的流程分析完毕
步骤2 AMS到ApplicationThread
2.1 AMS.startActivityAsUser():
@Override
public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
// 分析1
userId = mUserController.handleIncomingUser(Binder.