ActivityStart

当手指点击了桌面的App图标时

当手指点击了桌面的App图标时发生了什么

startActivity_onCreate.jpg

当手指点击了桌面的App图标时发生了什么 - ProcessOn

普通Activity启动整体流程.png

AIDL:

  • frameworks/base/core/java/android/app/IActivityManager.aidl ->

    本地进程获取system_server进程中的AMS服务

  • frameworks/base/core/java/android/app/IApplicationThread.aidl ->

    system_server进程调用本地进程ActivtyThread执行 (如LaunchActivityItem创建Activity与ResumeActivityItem走onResume生命周期) 事务发送Handler消息

0x01: 从activity.startActivity到onPuase()

Activity请求AMS流程.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
android.app.Activity#startActivity(android.content.Intent)

->

android.app.Activity#startActivityForResult(android.content.Intent, int, android.os.Bundle)
...
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
...

->

android.app.Instrumentation#execStartActivity(android.content.Context, android.os.IBinder, android.os.IBinder, android.app.Activity, android.content.Intent, int, android.os.Bundle)

// Instrumentation#execStartActivity各个版本实现区别较大,Android8之前(不含)是用ActivityManagerProxy代理AMS,Android8及之后替换为直接以AIDL方法获取调用AMS服务,Android10及之后又将将该过程交给ActivityTaskMangerService(ATMS)执行startActivity流程

int result = ActivityManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target, requestCode, 0, null, options);
checkStartActivityResult(result, intent);
->


com.android.server.am.ActivityManagerService#startActivity(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,int startFlags, ProfilerInfo profilerInfo, Bundle bOptions)

//Android9.0即API28为例:经过上述IPC过程后,进入system_server进程,接下来在ActivityManagerService中执行startActivity

————— 应用进程与system_server进程切换的分割线 —————-
切换到system_server进程,由于system_server是隐藏的,故难以debug


AMS处理启动Activity流程.png

一、ActivityManagerService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//以下为system_server中进行的AMS startActivity的流程


com.android.server.am.ActivityManagerService#startActivity(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,int startFlags, ProfilerInfo profilerInfo, Bundle bOptions)

->

com.android.server.am.ActivityManagerService#startActivity

return startActivityAsUser(... ,
UserHandle.getCallingUserId());

->

com.android.server.am.ActivityManagerService#startActivityAsUser(IApplicationThread, java.lang.String, android.content.Intent, java.lang.String, android.os.IBinder, java.lang.String, int, int, ProfilerInfo, android.os.Bundle, int)

->

com.android.server.am.ActivityManagerService#startActivityAsUser(IApplicationThread, java.lang.String, android.content.Intent, java.lang.String, android.os.IBinder, java.lang.String, int, int, ProfilerInfo, android.os.Bundle, int, boolean)

...
// TODO: Switch to user app stacks here.
return mActivityStartController.obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setResolvedType(resolvedType)
.setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(bOptions)
.setMayWait(userId)
.execute();
...

->

com.android.server.am.ActivityStartController#obtainStarter

//工厂模式,制造一个核心启动类ActivityStarter,赋值startActivityAsUser入参给ActivityStarter,并叼调用其execute

->

二、ActivityStarter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//ActivityStarter.java 


com.android.server.am.ActivityStarter#execute() {
...
//mRequest.mayWait == true
return startActivityMayWait(mRequest.caller, mRequest.callingUid,
mRequest.callingPackage, mRequest.intent, mRequest.resolvedType,
mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,
mRequest.resultWho, mRequest.requestCode, mRequest.startFlags,
mRequest.profilerInfo, mRequest.waitResult, mRequest.globalConfig,
mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.userId,
mRequest.inTask, mRequest.reason,
mRequest.allowPendingRemoteAnimationRegistryLookup);
...
}

//ActivityStarter#startActivityMayWait 中new 一个ActivityRecord, ActivityRecord 为Activity 在AMS中的信息,然后在下面的startActivity中赋值。并且找到Activity 所在的ActivityStack.

private int startActivityMayWait(IApplicationThread caller, int callingUid,
String callingPackage, Intent intent, String resolvedType,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode, int startFlags,
ProfilerInfo profilerInfo, WaitResult outResult,
Configuration globalConfig, SafeActivityOptions options, boolean ignoreTargetSecurity,
int userId, TaskRecord inTask, String reason,
boolean allowPendingRemoteAnimationRegistryLookup) {
//...
synchronized (mService) {
final ActivityStack stack = mSupervisor.mFocusedStack;
//...
final ActivityRecord[] outRecord = new ActivityRecord[1];
int res = startActivity(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo,
voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid,
callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options,
ignoreTargetSecurity, componentSpecified, outRecord, inTask, reason,
allowPendingRemoteAnimationRegistryLookup);
}

-> startActivity -> startActivity -> startActivity

->
//经过多个startACtivity重载方法套娃式调用(主要是Activity信息的组装)后,走到startActivityUnchecked

//startActivityUnchecked 中调用ActivityStack#startActivityLocked 找到Activity 所在的TaskRecord, 把Activity 插入TaskRecord 合适的位置。调用ActivityStackSupervisor#resumeFocusedStackTopActivityLocked

private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity) {

//...

mTargetStack.startActivityLocked(mStartActivity, topFocused, newTask, mKeepCurTransition,mOptions);
//...
if (mDoResume) {
if (!mTargetStack.isFocusable()
|| (topTaskActivity != null && topTaskActivity.mTaskOverlay
&& mStartActivity != topTaskActivity)) {
} else {
mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity, mOptions);
}
} else if (mStartActivity != null) {
}
//...
return START_SUCCESS;
}

三、ActivityStackSupervisor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//ActivityStackSupervisor.java
//Activity 已经在进入ActivityStack, 然后从Activity 中查找这个Activity 然后调状态设置为resume.
boolean resumeFocusedStackTopActivityLocked(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {

if (!readyToResume()) {
return false;
}

if (targetStack != null && isFocusedStack(targetStack)) {
return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
}

return false;
}

四、ActivityStack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//ActivityStack.java
//继续回到 ActivityStack resume Activity.在resumeTopActivityInnerLocked 中先检查桌面的Activity 状态,桌面Activity 状态需要设置为pause, startPausingLocked 把桌面Activity设置为paused。 由于新的Activity 在另外一个进程中,另外一个进程还没有启动,mStackSupervisor.startSpecificActivityLocked 启动新的Activity 进程。

boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
//...
try {
// Protect against recursion.
mStackSupervisor.inResumeTopActivity = true;
result = resumeTopActivityInnerLocked(prev, options);

//...
return result;
}

private boolean resumeTopActivityInnerLocked(ActivityRecord prev,ActivityOptions options) {
//...
boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
if (mResumedActivity != null) {
if (DEBUG_STATES) Slog.d(TAG_STATES,
"resumeTopActivityLocked: Pausing " + mResumedActivity);
//启动方Activity 状态需要设置为pause
pausing |= startPausingLocked(userLeaving, false, next, false);
}
//...
if (next.app != null && next.app.thread != null) {
//...
} else {
//...
mStackSupervisor.startSpecificActivityLocked(next, true, true);
}

return true;
}

//关于PauseActivityItem事务执行流程可参见
//@0x02——一、releaseStartActivityLocked——1、创建添加activity事务并通过binder将事务交予应用进程执行
final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
ActivityRecord resuming, boolean pauseImmediately) {
//...
if (prev.app != null && prev.app.thread != null) {
mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,PauseActivityItem.obtain(prev.finishing, userLeaving, prev.configChangeFlags, pauseImmediately));
} catch (Exception e) {

}
} else {

}
}

小结:

从activity.startActivity()到Instrunmentaion中以Binder机制通过ActivityManager.getService().startActivity切换到system_server进程中的ActivityMangerService服务,再到ActivityMangerService中ActivityStarter、ActivityStatckSuperVisor、ActivityStack执行解析生成ActivityRecord(包含AMS、activityInfo、Configuration等信息),最后由ActivityStack通过binder将”pause事务”从AMS传递到应用进程,应用进程handler机制处理该事务,旧Activity pause成功。开始AMS启动新Activity。

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
至此的链路简述
Activity.startActivity->
Instrumentation.execStartActivity->
ActivityManager.getService().startActivity->
ActivityManagerService.startActivityAsUser->
ActivityStarter.execute->
ActivityStarter.startActivityMayWait ->
ActivityStarter.startActivity->ActivityStarter.startActivity重载套娃->
ActivityStarter.startActivityUnchecked->
ActivityStackSupervisor.resumeFocusedStackTopActivityLocked->
ActivityStack.resumeTopActivityUncheckedLocked->

ActivityStack.resumeTopActivityInnerLocked
{
//startPausingLocked()
//startSpecificActivityLocked(next, true, false);
}

至此,新Activity的ActivityRecord(AMS中的Activity信息,包括不限于ActivityInfo、ProcessRecord、Configuration,ActivityStackSupervisor、发起跳转的Activity的ActivityRecord和resultTo的ActivityRecord)创建解析完成,旧Activity的pause生命周期被调用执行(见附录ActivityStack#startPausingLocked),接下来就是通过AMS启动新的Activity

0x02: AMS启动新的Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
com.android.server.am.ActivityStackSupervisor#startSpecificActivityLocked (...) {
ProcessRecord app = mService.getProcessRecordLocked(r.processName,
r.info.applicationInfo.uid, true);
getLaunchTimeTracker().setLaunchTime(r);
if (app != null && app.thread != null) {
try {
// !!! 看这里,如果没有另开进程的标识,就走realStartActivityLocked,直接走普通Activity启动流程,然后retrun !!!
if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
|| !"android".equals(r.info.packageName)) {
// Don't add this if it is a platform co mponent that is marked
// to run in multiple processes, because this is actually
// part of the framework so doesn't make sense to track as a
// separate apk in the process.
app.addPackage(r.info.packageName,r.info.applicationInfo.longVersionCode,
mService.mProcessStats);
}
realStartActivityLocked(r, app, andResume, checkConfig);
return;
} catch (RemoteException e) {
Slog.w(TAG, "Exception when starting activity "
+ r.intent.getComponent().flattenToShortString(), e);
}
// If a dead object exception was thrown -- fall through to
// restart the application.
}
// !!!看这里,如果没有在前面被return掉,就会走新开进程启动Activity的流程
mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
"activity", r.intent.getComponent(), false, false, true);
}

一、startProcessLocked

简述:启动新的进程,即Zygote进程Process.start() fork出新的进程并反射调用其mian方法,走到ActivityThread.main,main方法中主要是初始化looper并启动循环,接着调用ActivityThread.attach()在该方法中初始化Application单例。接下来就是二、实质性启动Activity。

启动新进程承载新Activity,之后继续走realStartActivityLocked常见同进程Activity启动的流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
com.android.server.am.ActivityManagerService#startProcessLocked(java.lang.String, android.content.pm.ApplicationInfo, boolean, int, java.lang.String, android.content.ComponentName, boolean, boolean, boolean) ->

com.android.server.am.ActivityManagerService#startProcessLocked(java.lang.String, java.lang.String, java.lang.String, com.android.server.am.ProcessRecord, int, int[], int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long) ->

com.android.server.am.ActivityManagerService#startProcess(...) {
if (hostingType.equals("webview_service")) {
startResult = startWebView(entryPoint,
app.processName, uid, uid, gids, runtimeFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
app.info.dataDir, null,
new String[] {PROC_START_SEQ_IDENT + app.startSeq});
} else {
startResult = Process.start(entryPoint,
app.processName, uid, uid, gids, runtimeFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
app.info.dataDir, invokeWith,
new String[] {PROC_START_SEQ_IDENT + app.startSeq});
}
}

->
//android.os.Process.java

public static final ZygoteProcess zygoteProcess =
new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET);

public static final ProcessStartResult start(final String processClass,
final String niceName,
int uid, int gid, int[] gids,
int runtimeFlags, int mountExternal,
int targetSdkVersion,
String seInfo,
String abi,
String instructionSet,
String appDataDir,
String invokeWith,
String[] zygoteArgs) {
return zygoteProcess.start(processClass, niceName, uid, gid, gids,
runtimeFlags, mountExternal, targetSdkVersion, seInfo,
abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
}

public final Process.ProcessStartResult start(final String processClass,
final String niceName,
int uid, int gid, int[] gids,
int runtimeFlags, int mountExternal,
int targetSdkVersion,
String seInfo,
String abi,
String instructionSet,
String appDataDir,
String invokeWith,
String[] zygoteArgs) {
try {
return startViaZygote(processClass, niceName, uid, gid, gids,
runtimeFlags, mountExternal, targetSdkVersion, seInfo,
abi, instructionSet, appDataDir, invokeWith, false /* startChildZygote */,
zygoteArgs);
} catch (ZygoteStartFailedEx ex) {
Log.e(LOG_TAG,
"Starting VM process through Zygote failed");
throw new RuntimeException(
"Starting VM process through Zygote failed", ex);
}
}

private Process.ProcessStartResult startViaZygote(final String processClass,
final String niceName,
final int uid, final int gid,
final int[] gids,
int runtimeFlags, int mountExternal,
int targetSdkVersion,
String seInfo,
String abi,
String instructionSet,
String appDataDir,
String invokeWith,
boolean startChildZygote,
String[] extraArgs)
throws ZygoteStartFailedEx {
ArrayList<String> argsForZygote = new ArrayList<String>();

// --runtime-args, --setuid=, --setgid=,
// and --setgroups= must go first
argsForZygote.add("--runtime-args");
argsForZygote.add("--setuid=" + uid);
argsForZygote.add("--setgid=" + gid);
argsForZygote.add("--runtime-flags=" + runtimeFlags);
if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) {
argsForZygote.add("--mount-external-default");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) {
argsForZygote.add("--mount-external-read");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) {
argsForZygote.add("--mount-external-write");
}
argsForZygote.add("--target-sdk-version=" + targetSdkVersion);

// --setgroups is a comma-separated list
if (gids != null && gids.length > 0) {
StringBuilder sb = new StringBuilder();
sb.append("--setgroups=");

int sz = gids.length;
for (int i = 0; i < sz; i++) {
if (i != 0) {
sb.append(',');
}
sb.append(gids[i]);
}

argsForZygote.add(sb.toString());
}

if (niceName != null) {
argsForZygote.add("--nice-name=" + niceName);
}

if (seInfo != null) {
argsForZygote.add("--seinfo=" + seInfo);
}

if (instructionSet != null) {
argsForZygote.add("--instruction-set=" + instructionSet);
}

if (appDataDir != null) {
argsForZygote.add("--app-data-dir=" + appDataDir);
}

if (invokeWith != null) {
argsForZygote.add("--invoke-with");
argsForZygote.add(invokeWith);
}

if (startChildZygote) {
argsForZygote.add("--start-child-zygote");
}

argsForZygote.add(processClass);

if (extraArgs != null) {
for (String arg : extraArgs) {
argsForZygote.add(arg);
}
}

synchronized(mLock) {
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
}
}

// 通过socket 检查新进程的启动情况
private static Process.ProcessStartResult zygoteSendArgsAndGetResult(
ZygoteState zygoteState, ArrayList<String> args)
throws ZygoteStartFailedEx {
try {

final BufferedWriter writer = zygoteState.writer;
final DataInputStream inputStream = zygoteState.inputStream;

writer.write(Integer.toString(args.size()));
writer.newLine();

for (int i = 0; i < sz; i++) {
String arg = args.get(i);
writer.write(arg);
writer.newLine();
}

writer.flush();

// Should there be a timeout on this?
Process.ProcessStartResult result = new Process.ProcessStartResult();

// Always read the entire result from the input stream to avoid leaving
// bytes in the stream for future process starts to accidentally stumble
// upon.
result.pid = inputStream.readInt();
result.usingWrapper = inputStream.readBoolean();

if (result.pid < 0) {
throw new ZygoteStartFailedEx("fork() failed");
}
return result;
} catch (IOException ex) {
zygoteState.close();
throw new ZygoteStartFailedEx(ex);
}
}

1、 Zygote 进程fork

Zygote 的fork 过程参考SystemService 的启动流程。Zygote fork 后也是通过反射的方法找到一个main 函数,这时候因为启动的是App 进程。所以这个main 函数为ActivityThread 的main函数。

start_app_process

  1. App发起进程:当从桌面启动应用,则发起进程便是Launcher所在进程;当从某App内启动远程进程,则发送进程便是该App所在进程。发起进程先通过binder发送消息给system_server进程;
  2. system_server进程:调用Process.start()方法,通过socket向zygote进程发送创建新进程的请求;
  3. zygote进程:在执行ZygoteInit.main()后便进入runSelectLoop()循环体内,当有客户端连接时便会执行ZygoteConnection.runOnce()方法,再经过层层调用后fork出新的应用进程;
  4. 新进程:执行handleChildProc方法,最后调用ActivityThread.main()方法。

2、 fork 后的新进程的 ActivityThread

fork 以后启动ActivityThread 的main 函数,在Main 函数中完成Looper的初始化,最重要的一个调用就是 attach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static void main(String[] args) {

Process.setArgV0("<pre-initialized>");

Looper.prepareMainLooper();

ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq);

if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}

Looper.loop();

throw new RuntimeException("Main thread loop unexpectedly exited");
}

private void attach(boolean system, long startSeq) {
sCurrentActivityThread = this;
mSystemThread = system;
if (!system) {

android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
UserHandle.myUserId());
RuntimeInit.setApplicationObject(mAppThread.asBinder());
final IActivityManager mgr = ActivityManager.getService();
try {
mgr.attachApplication(mAppThread, startSeq);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
} else {

}
}

AMS 的 attachApplication

1
2
IActivityManager mgr = ActivityManager.getService();
mgr.attachApplication(mAppThread, startSeq);

AMS 的attachApplication 依次调用了ApplicationThread 的

  1. bindApplication // 创建Application
  2. scheduleTransaction(clientTransaction); // Resume Activity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
ActivityMangerService.class:
@Override
public final void attachApplication(IApplicationThread thread, long startSeq) {
synchronized (this) {
attachApplicationLocked(thread, callingPid, callingUid, startSeq);
}
}


@GuardedBy("this")
private final boolean attachApplicationLocked(IApplicationThread thread,
int pid, int callingUid, long startSeq) {


// It's possible that process called attachApplication before we got a chance to
// update the internal state.

try {
if (app.isolatedEntryPoint != null) {

} else if (app.instr != null) {
thread.bindApplication(processName, appInfo, providers,
app.instr.mClass,
profilerInfo, app.instr.mArguments,
app.instr.mWatcher,
app.instr.mUiAutomationConnection, testMode,
mBinderTransactionTrackingEnabled, enableTrackAllocation,
isRestrictedBackupMode || !normalMode, app.persistent,
new Configuration(getGlobalConfiguration()), app.compat,
getCommonServicesLocked(app.isolated),
mCoreSettingsObserver.getCoreSettingsLocked(),
buildSerial, isAutofillCompatEnabled);
} else {
thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
null, null, null, testMode,
mBinderTransactionTrackingEnabled, enableTrackAllocation,
isRestrictedBackupMode || !normalMode, app.persistent,
new Configuration(getGlobalConfiguration()), app.compat,
getCommonServicesLocked(app.isolated),
mCoreSettingsObserver.getCoreSettingsLocked(),
buildSerial, isAutofillCompatEnabled);
}

} catch (Exception e) {

}

// See if the top visible activity is waiting to run in this process...
if (normalMode) {
try {
if (mStackSupervisor.attachApplicationLocked(app)) {
didSomething = true;
}
} catch (Exception e) {

}
}

return true;
}
boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
final String processName = app.processName;
boolean didSomething = false;
for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
final ActivityStack stack = display.getChildAt(stackNdx);
if (!isFocusedStack(stack)) {
continue;
}
stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
final ActivityRecord top = stack.topRunningActivityLocked();
final int size = mTmpActivityList.size();
for (int i = 0; i < size; i++) {
final ActivityRecord activity = mTmpActivityList.get(i);
if (activity.app == null && app.uid == activity.info.applicationInfo.uid
&& processName.equals(activity.processName)) {
try {
if (realStartActivityLocked(activity, app,
top == activity /* andResume */, true /* checkConfig */)) {
didSomething = true;
}
} catch (RemoteException e) {
throw e;
}
}
}
}
}
return didSomething;
}

realStartActivityLocked 见下

二、realStartActivityLocked

简述:实质性启动Activity,首先AMS推动包含Launch事务和Resume生命周期事务的ClientTransaction,App进程在ActivityThread的handler中接受并处理,调用handleLaunchActivity()->perfomLaunchActivity(),此方法中,首先反射创建了Activity对象,并调用了activity.attach(),之后执行onCreate。

1、创建添加activity事务并通过binder将事务交予应用进程执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//com.android.server.am.ActivityStackSupervisor#realStartActivityLocked

//主要创建事务交给本地执行
final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
boolean andResume, boolean checkConfig) throws RemoteException {
...
//创建启动activity的事务ClientTransaction对象
// Create activity launch transaction.
final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
r.appToken);
// 添加LaunchActivityItem,该类obtain入参都是应用进程中创建Activity需要的所有信息,到时候会将这些信息组装成ActivityClientRecord交给ActivityThread的performLaunchActivity使用
clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
System.identityHashCode(r), r.info,
mergedConfiguration.getGlobalConfiguration(),
mergedConfiguration.getOverrideConfiguration(), r.compat,
r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
r.persistentState, results, newIntents, mService.isNextTransitionForward(),
profilerInfo));

// Set desired final state.
//添加执行Resume事务ResumeActivityItem,后续会在本地被执行
final ActivityLifecycleItem lifecycleItem;
if (andResume) {
lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward());
} else {
lifecycleItem = PauseActivityItem.obtain();
}
clientTransaction.setLifecycleStateRequest(lifecycleItem);

// ClientLifecycleManager.scheduleTransaction就是调用clientTransaction的schedule启动事务然后回收clientTransaction
// 这里的mService就是AMS
// 记住上面两个item:LaunchActivityItem和ResumeActivityItem,这是事务的执行单位
mService.getLifecycleManager().scheduleTransaction(clientTransaction);
}

->

com.android.server.am.ClientLifecycleManager#scheduleTransaction(ClientTransaction) {

//把事务交给本地ActivityThread执行。这里通过本地ApplicationThread在服务端的接口IApplicationThread来进行跨进程通信。后面的逻辑就回到了应用程序进程了。

void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
final IApplicationThread client = transaction.getClient();
transaction.schedule();
if (!(client instanceof Binder)) {
transaction.recycle();
}
}
}

->

//这里的IApplicationThread是要启动进程的 IBinder 接口
//ApplicationThread是ActivityThread的内部类,IApplicationThread是IBinder代理接口
//这里将逻辑转到应用进程中执行
private IApplicationThread mClient;
public void schedule() throws RemoteException {
mClient.scheduleTransaction(this);
}

————— system_server进程与应用进程切换的分割线 —————-
通过AIDL方式又回到应用进程了,调用了IApplicationThread.scheduleTransaction()


2、回到应用进程后ActivityThread执行事务创建Activity

ActivityThread响应启动Activity流程.png

ApplicationThread.class是 “AIDL接口—— IApplicationThread接口具体实现”,是ActivityThread的内部类,其定义了服务的远程过程调用 (RPC) 接口,即本地进程提供给其他进程通过IPC机制调用的接口:如更新进程状态updateProcessState,scheduleTransaction

System private API for communicating with the application. This is given to the activity manager by an application when it starts up, for the activity manager to tell the application about things it needs to do.

用于与应用程序通信的系统私有API。 这是应用程序在启动时给AMS的,以便AMS告诉应用程序它需要做的事情。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

//ActivityThread#ApplicationThread.class
//ApplicationThread extends IApplicationThread.Stub
public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
//ActivityThread 继承 ClientTransactionHandler 的scheduleTransaction方法
ActivityThread.this.scheduleTransaction(transaction);
}


//abstract ClientTransactionHandler.class
void scheduleTransaction(ClientTransaction transaction) {
//主要是更新应用进程状态see@ LaunchActivityItem、ResumeActivityItem preExecute
transaction.preExecute(this);

//通过Handler发送what为EXECUTE_TRANSACTION,obj为transaction的消息,即ActivityThread中Hanlder实现H.class处理
sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
}

abstract void sendMessage(int what, Object obj);


//ActivityThread.class
/* */
final H mH = new H();
private final TransactionExecutor mTransactionExecutor = new TransactionExecutor(this);

void sendMessage(int what, Object obj) {
sendMessage(what, obj, 0, 0, false);
}

private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
if (DEBUG_MESSAGES) Slog.v(
TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
+ ": " + arg1 + " / " + obj);
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
msg.arg1 = arg1;
msg.arg2 = arg2;
if (async) {
msg.setAsynchronous(true);
}
mH.sendMessage(msg);
}

//class H extends Handler
public static final int EXECUTE_TRANSACTION = 159;

public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
case EXECUTE_TRANSACTION:
final ClientTransaction transaction = (ClientTransaction) msg.obj;
mTransactionExecutor.execute(transaction);
if (isSystem()) {
transaction.recycle();
}
break;
}
}

//其实还是那句话,事务(LaunchActivityItem与ResumeActivityItem)从system_server中通过AIDL的IPC方式,丢到应用进程中的ActivityThread的handler中执行事务的execute方法。
a. LaunchActivityItem/ResumeActivityItem preExecute/execute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* 见上
ActivityStackSupervisor.realStartActivityLocked()中 {
ClientTransaction.addCallback(LaunchActivityItem...)
clientTransaction.setLifecycleStateRequest(ResumeActivityItem实例lifecycleItem);
}
//ClientTransaction.class
public void setLifecycleStateRequest(ActivityLifecycleItem stateRequest) {
mLifecycleStateRequest = stateRequest;
}
public void addCallback(ClientTransactionItem activityCallback) {
if (mActivityCallbacks == null) {
mActivityCallbacks = new ArrayList<>();
}
mActivityCallbacks.add(activityCallback);
}
*/

//ClientTransaction.java;
public void preExecute(android.app.ClientTransactionHandler clientTransactionHandler) {
if (mActivityCallbacks != null) {
final int size = mActivityCallbacks.size();
for (int i = 0; i < size; ++i) {
mActivityCallbacks.get(i).preExecute(clientTransactionHandler, mActivityToken);
}
}
if (mLifecycleStateRequest != null) {
mLifecycleStateRequest.preExecute(clientTransactionHandler, mActivityToken);
}
}

//TransactionExecutor.class
//(其实就是ActivityThread,依赖倒置)
private ClientTransactionHandler mTransactionHandler;
public TransactionExecutor(ClientTransactionHandler clientTransactionHandler) {
mTransactionHandler = clientTransactionHandler;
}

public void execute(ClientTransaction transaction) {
final IBinder token = transaction.getActivityToken();
log("Start resolving transaction for client: " + mTransactionHandler + ", token: " + token);

executeCallbacks(transaction);

executeLifecycleState(transaction);
mPendingActions.clear();
log("End resolving transaction");
}
->

public void executeCallbacks(ClientTransaction transaction) {
final int size = callbacks.size();
for (int i = 0; i < size; ++i) {
final ClientTransactionItem item = callbacks.get(i);
item.execute(mTransactionHandler, token, mPendingActions);
item.postExecute(mTransactionHandler, token, mPendingActions);
}
}

private void executeLifecycleState(ClientTransaction transaction) {
final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest();
lifecycleItem.execute(mTransactionHandler, token, mPendingActions);
lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);
}

//preExecute在execute前准备客户端请求。这方面的一个例子可能是通知某些值的挂起更新。ps:更新进程状态
//execute执行请求
//postExecute执行execute后需要执行的所有操作,例如将结果报告给服务器。


//LaunchActivityItem.class extends ClientTransactionItem (Parcelable implementation)
@Override
public void preExecute(ClientTransactionHandler client, IBinder token) {
//
client.updateProcessState(mProcState, false);
client.updatePendingConfiguration(mCurConfig);
}

public void execute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) {
Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart");

//ActivityClientRecord就是应用进程中的Activity信息,与之对应的是system_server中ActivityRecord类,是系统进程中的Activity信息
ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo,
mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState,mPendingResults, mPendingNewIntents, mIsForward,mProfilerInfo, client);

//这里的client其实ActivityThread
client.handleLaunchActivity(r, pendingActions, null /* customIntent */);

Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
}

//ResumeActivityItem.class extends ClientTransactionItem ActivityLifecycleItem extends ClientTransactionItem (Parcelable implementation)
@Override
public void preExecute(ClientTransactionHandler client, IBinder token) {
if (mUpdateProcState) {
client.updateProcessState(mProcState, false);
}
}
@Override
public void execute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) {
Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
client.handleResumeActivity(token, true /* finalStateRequest */, mIsForward,
"RESUME_ACTIVITY");
Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
}

以启动activity的LaunchActivityItem.execute()中
client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
为核心分析应用进程中ActivityThread中启动Activity的流程。

b. handleLaunchActivity()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//ActivityThread.class
@Override
public Activity handleLaunchActivity(ActivityClientRecord r,
PendingTransactionActions pendingActions, Intent customIntent) {
// Initialize before creating the activity
if (!ThreadedRenderer.sRendererDisabled) { //非系统进程才能渲染
GraphicsEnvironment.earlyInitEGL();
}
WindowManagerGlobal.initialize();
final Activity a = performLaunchActivity(r, customIntent);
}
->

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
//package信息
ActivityInfo aInfo = r.activityInfo;
if (r.packageInfo == null) {
r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
Context.CONTEXT_INCLUDE_CODE);
}
// 获取activity的包名类型信息
ComponentName component = r.intent.getComponent();
if (component == null) {
component = r.intent.resolveActivity(
mInitialApplication.getPackageManager());
r.intent.setComponent(component);
}
if (r.activityInfo.targetActivity != null) {
component = new ComponentName(r.activityInfo.packageName,
r.activityInfo.targetActivity);
}

// 创建context上下文
ContextImpl appContext = createBaseContextForActivity(r);

// 反射创建Activity对象(工厂模式)
Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.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);
}

try {
//获取Application单例(在ActivityThread.attach时创建)
Application app = r.packageInfo.makeApplication(false, mInstrumentation);



if(activity != null) {
// 为Activity添加window,之后移除对象
Window window = null;
if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
window = r.mPendingRemoveWindow;
r.mPendingRemoveWindow = null;
r.mPendingRemoveWindowManager = null;
}
//onAttach
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, r.configCallback);

//onCreate
mInstrumentation.callActivityOnCreate(activity, r.state);

}
}
}

//LoadedApk.class
public Application makeApplication(boolean forceDefaultAppClass,Instrumentation instrumentation) {
//只有一个application,在应用初始化时才会走到此方法后面的流程
if (mApplication != null) {
return mApplication;
}

Application app = null;

try {
java.lang.ClassLoader cl = getClassLoader();
ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);

//newApplication反射创建Application(工厂模式)后会顺手调一下app.attach(context);
app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);

appContext.setOuterContext(app);
} catch (Exception e) {
//...
}

if (instrumentation != null) {
try {
//具体实现就app.onCreate();
instrumentation.callApplicationOnCreate(app);
} catch (Exception e) {
if (!instrumentation.onException(app, e)) {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
throw new RuntimeException(
"Unable to create application " + app.getClass().getName()
+ ": " + e.toString(), e);
}
}
}
}

003:Activity.Attach()

简述:首先初始化Window,具体实现类是PhoneWindow。为其设置WindowManger(具体实现类为WindowManagerImpl)

在Activity的attach方法中,很关键的一点就是初始化Window,从这里就能看到,Window的实现类,是PhoneWindow。PhoneWindow的创建对于我们后面的操作很重要。

mWindow是一个Window类型的变量,但实际上它是一个PhoneWindow对象,与Activity的内容显示相关。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config) {
attachBaseContext(context);

mFragments.attachActivity(this, mContainer, null);

mWindow = PolicyManager.makeNewWindow(this);

... //将各种参数赋给Activity的成员变量

mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}

004:Activity.onCreate()

简述:onCreate()->setContentView()->PhoneWindow.setContentView(),在这里初始化了最顶层View(FrameLayout)DecorView;并将layoutResID通过LayoutInflate填充后添加到DecorView中。

1
2
3
4
5
6
7
8
9
10
11
12
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mContentParent.addView(view, params);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}

005:ActivityThread.handleResumeActivity()

简述:

核心就是先调用onResume(),

然后再调用Window#addView(Window也就是上面activity.attach()中初始化的PhoneWindow)。接下来就是走到WindowManager(具体实现在WindowManagerGlobal中)的addView()。首先创建ViewRootImpl然后调用它的setView,setView中最关键的就是就是调用了requestLayout()然后注册Vsync回调回来后ViewRootImpl.performTraversals(),其中先执行了dispatchAttachToWindow(View.post的缓存action执行),后走了performMeasure、performLayout、performDraw测绘三大流程

  • performResumeActivity

  • Window#addView

    new ViewRootImpl()

    ViewRootImpl.setView()

    ViewRootImpl.requestLayout()

    ViewRootImpl.scheduleTraversals()

    ViewRootImpl.doTraversal();

    ViewRootImpl.performTraversals();

0xFF: 附录

附录一 execStartActivity

Android 6.0/9.0/10.0 Instrumentation#execStartActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
...
//Android 6
//ActivityManagerNative.getDefault()拿到的对象是ActivityManagerProxy,这个类初始化时即构造传入AMS(ServiceManager.getService("activity")),由ActivityManagerProxy代理AMS执行startActivity方法
ActivityManagerNative.getDefault()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);

/**
* Retrieve the system's default/global activity manager.
*/
static public IActivityManager getDefault() {
return gDefault.get();
}
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
protected IActivityManager create() {
IBinder b = ServiceManager.getService("activity");
...
IActivityManager am = asInterface(b);
...
return am;
}
};

//Android 9 ActivityManagerProxy类已经被去掉了, ActivityManger.getService 获取 AMS 的单例实例,然后调用其 startActivity 方法,实际上这里就是通过 AIDL 来调用 AMS 的 startActivity 方法。

ActivityManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);

public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}

private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};

//Andorid 10 新增了一层ActivityTaskManager来管理activity任务,AIDL方式向ServiceManager获取ACTIVITY_TASK_SERVICE服务单例,其具有IActivityTaskManager的方法实现(包括startActivity)
int result = ActivityTaskManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);


android.app.ActivityTaskManager#getService
...
@UnsupportedAppUsage(trackingBug = 129726065)
private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton =
new Singleton<IActivityTaskManager>() {
@Override
protected IActivityTaskManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
return IActivityTaskManager.Stub.asInterface(b);
}
};

附录二 startPausingLocked

ActivityStack#startPausingLocked

1
2
3
4
5
6
7
8
9
10
11
ActivityStack#startPausingLocked
{
//...
//PauseActivityItem继承于ActivityLifecycleItem,又继承于ClientTransactionItem,是Activity负责暂停的事务类。同样继承于ActivityLifecycleItem的负责Activity生命周期的类还有ResumeActivityItem、StopActivityItem等。ActivityLifecycleItem相比其父类多了状态的请求(事务执行后客户端活动应该处于的最后生命周期状态)
mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken, PauseActivityItem.obtain(prev.finishing, userLeaving, prev.configChangeFlags, pauseImmediately));
//...
}

->


ActivityManager : Display

ActivityManager : Display / startActivity Android6/7

简单结论:display 只统计A onPause之后

AMS 启动新ActivityB 并 执行 B的onCreate、onstart、onresume 与 B向WMS注册窗口到编舞者发起的第一次测绘 完成

Activity的启动可以分为三个步骤,以ActivityA启动ActivityB为例,三步骤分别为:

  1. 以ActivityA调用startActivity,到ActivityA成功pause为止

    displayTimeStart

  2. ActivityB成功初始化,到执行完resume为止

  3. ActivityB向WSM注册窗口,到第一帧绘制完成为止
    displayTimeEnd

ActiivtyA Pause流程

当ActivityA使用startActivity方法启动ActivityB时,执行函数链路如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Android 6.0/7.0
ActivityA.startActivity->
Instrumentation.execStartActivity->
ActivityManagerNative.getDefault.startActivity->
ActivityManagerService.startActivityAsUser->
ActivityStarter.startActivityMayWait ->
ActivityStarter.startActivityLocked ->
ActivityStarter.startActivityUnchecked->
ActivityStackSupervisor.resumeFocusedStackTopActivityLocked->
ActivityStack.resumeTopActivityUncheckedLocked
->

ActivityStack.resumeTopActivityInnerLocked
{
//startPausingLocked() 暂停旧Activity
//startSpecificActivityLocked(next, true, false);
}

->
ActivityStack.startPausingLocked
->

ActivityThread#ApplicationThread.schedulePauseActivity->
ActivityThread.handlePauseActivity->
└ActivityA.onPause
ActivityManagerNative.getDefault().activityPaused

当App请求AMS要启动一个新页面的时候,AMS首先会pause掉当前正在显示的Activity,当然,这个Activity可能与请求要开启的Activity不在一个进程,比如点击桌面图标启动App,当前要暂停的Activity就是桌面程序Launcher。在onPause内执行耗时操作是一种很不推荐的做法,从上述调用链路可以看出,如果在onPause内执行了耗时操作,会直接影响到ActivityManagerNative.getDefault().activityPaused()方法的执行,而这个方法的作用就是通知AMS,“当前Activity已经已经成功暂停,可以启动新Activity了”。

ActivityB Launch流程

在AMS接收到App进程对于activityPaused方法的调用后,执行函数链路如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//ActivityStack.resumeTopActivityInnerLocked 
->
ActivityStackSupervisor.startSpecificActivityLocked->
->

//displayStartTime
1.启动新进程:ActivityManagerService.startProcessLocked 暂不展开
2.当前进程:ActivityStackSupervisor.realStartActivityLocked->
ActivityThread?ApplicationThread.scheduleLaunchActivity->
Activity.handleLaunchActivity->
└Activity.onCreate
└Activity.onRestoreInstanceState
└handleResumeActivity
└Activity.onStart->
└Activity.onResume->
└WindowManager.addView->

AMS在经过一系列方法调用后,通知App进程正式启动一个Actviity,注意如果要启动Activity所在进程不存在,比如点击桌面图标第一次打开应用,或者App本身就是多进程的,要启动的新页面处于另外一个进程,那就需要走到ActivityManagerService.startProcessLocked流程,等新进程启动完毕后再通知AMS,这里不展开。按照正常流程,会依次走过Activity生命周期内的onCreate、onRestoreInstanceState、onStart、onResume方法,这一步的耗时基本也可以看成就是这四个方法的耗时,由于这四个方法是同步调用的,所以可以通过以onCreate方法为起点,onResume方法为终点,统计出这一步骤的总耗时。

ActivityB Render流程

在ActivityB执行完onResume方法后,就可以显示该Activity了,调用流程如下

1
2
3
4
5
6
7
WindowManager.addView->
WindowManagerImpl.addView->
ViewRootImpl.setView->
ViewRootImpl.requestLayout->
└ViewRootImpl.scheduleTraversals->
└Choreographer.postCallback->
WindowManagerSerivce.add

这一步的核心实际上是Choreographer.postCallback,向Choreographer注册了一个回调,当Vsync事件到来时,就会执行下面的回调进行ui的渲染

1
2
3
4
5
6
7
8
9
ViewRootImpl.doTraversal->
ViewRootImpl.performTraversals->
└ViewRootImpl.relayoutWindow
└ViewRootImpl.performMeasure
└ViewRootImpl.performLayout
└ViewRootImpl.performDraw
ViewRootImpl.reportDrawFinished...

reportLaunchTimeLocked

这里分别执行了performMeasure、performLayout、performDraw,实际上就是对应到DecorView的测量、布局、绘制三个流程。由于Android的UI是个树状结构,作为根View的DecorView的测量、布局、绘制,会调用到所有子View相应的方法,因此,这一步的总耗时就是所有子View在测量、布局、绘制中的耗时之和,如果某个子View在这三个方法中如果进行了耗时操作,就会拖慢整个UI的渲染,进而影响Activity第一帧的渲染速度。

从Window视角看ActivityStart

当从Window的角度看待Activity的启动过程时,可以看到以下流程:

  1. 在目标进程中,执行Activity.handleLaunchActivity方法,会先获取AMS服务,之后调用Activity.performLaunchActivity

  2. 回调目标Activity的onAttach方法,初始化Window及Window相关对象,如PhoneWindow、Token等。 mWindow = new PhoneWindow(this, window, activityConfigCallback);

  3. 之后回调目标Activity的onCreate方法,然后通过setContentView方法创建一个DecorView对象;

  4. 在目标进程中,执行Activity.handleResumeActivity方法,会回调目标Activity的onResume方法,然后调用目标Activity的makeVisible方法;在makeVisible方法中,会调用WindowManagerImpl.addView方法绘制View,并通过Binder远程调用WMS添加Window。ViewManager wm = getWindowManager();wm.addView(mDecor, getWindow().getAttributes());

Author

white crow

Posted on

2021-07-30

Updated on

2024-03-25

Licensed under