应用内启动Activity流程图
1 Activity会执行的startActivity
有几种重载方法,但最终会调用startActivityForResult
2 Activity 的startActivityForResult
会调用Instrumentation的execStartActivity
方法
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
3 Instrumentation的execStartActivity
方法会调用 ActivityManagerNative的startActivity
int result = ActivityManagerNative.getDefault()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);
ps:这个方法最后还会执行
checkStartActivityResult
来检查是否启动成功。
4 ActivityManagerNative的gDefault
会通过Binder返回ActivityManagerService的单例。实际上是调用的是ActivityManagerService的startActivity
方法,会调用startActivityAsUser
startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
resultWho, requestCode, startFlags, profilerInfo, options,
UserHandle.getCallingUserId());
5 ActivityManagerService的` startActivityAsUser `会调用ActivityStackSupervisor的` startActivityMayWait `
mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
profilerInfo, null, null, options, userId, null, null);
6 ActivityStackSupervisor的startActivityMayWait
会调用startActivityLocked
int res = startActivityLocked(caller, intent, resolvedType, aInfo,
voiceSession, voiceInteractor, resultTo, resultWho,
requestCode, callingPid, callingUid, callingPackage,
realCallingPid, realCallingUid, startFlags, options,
componentSpecified, null, container, inTask);
7 ActivityStackSupervisor的startActivityLocked
会调用startActivityUncheckedLocked
err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, true, options, inTask);
8 ActivityStackSupervisor的startActivityUncheckedLocked
会调用ActivityStack的startActivityLocked
targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
9 ActivityStack的startActivityLocked
会ActivityStackSupervisor调用的resumeTopActivitiesLocked
mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
10 ActivityStackSupervisor的resumeTopActivitiesLocked
又会调用回ActivityStack的resumeTopActivityLocked
result = targetStack.resumeTopActivityLocked(target, targetOptions);
11 ActivityStack的resumeTopActivityLocked
又会调用自身的resumeTopActivityInnerLocked
result = resumeTopActivityInnerLocked(prev, options);
12 ActivityStack的resumeTopActivityInnerLocked
会调用自身的startPausingLocked
先暂停之前的Activity
pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
PS: 这里会结束掉这个resumeTopActivityInnerLocked,返回true
if (pausing) {
if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
"resumeTopActivityLocked: Skip resume: need to start pausing");
// At this point we want to put the upcoming activity's process
// at the top of the LRU list, since we know we will be needing it
// very soon and it would be a waste to let it get killed if it
// happens to be sitting towards the end.
if (next.app != null && next.app.thread != null) {
mService.updateLruProcessLocked(next.app, true, null);
}
if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
return true;
}
13 ActivityStack的startPausingLocked
会调用ActivityRecord的ProcessRecord
的IApplicationThread,通过Binder实际上调用ActivityThread的ApplicationThread的schedulePauseActivity
prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
userLeaving, prev.configChangeFlags, dontWait);
14 ActivityThread的ApplicationThread的schedulePauseActivity
发送消息最后会调用ActivityThread的handlePauseActivity
,最后会调用ActivityManagerService的activityPaused
PS:performPauseActivity会调用Activity的onPause
ActivityManagerNative.getDefault().activityPaused(token);
15 ActivityManagerService的activityPaused
会调用ActivityStack的activityPausedLocked
stack.activityPausedLocked(token, false);
16 用ActivityStack的activityPausedLocked
会调用自身的completePauseLocked
completePauseLocked(true);
17 用ActivityStack的completePauseLocked
又会调用ActivityStackSupervisor的resumeTopActivitiesLocked
mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
18 ActivityStackSupervisor的resumeTopActivitiesLocked
又会调用回ActivityStack的resumeTopActivityLocked
result = targetStack.resumeTopActivityLocked(target, targetOptions);
19 ActivityStack的resumeTopActivityLocked
又会调用自身的resumeTopActivityInnerLocked
result = resumeTopActivityInnerLocked(prev, options);
20 ActivityStack的resumeTopActivityInnerLocked
会调用ActivityStackSupervisor的startSpecificActivityLocked
if (next.app != null && next.app.thread != null) {
...
}else{
mStackSupervisor.startSpecificActivityLocked(next, true, true);
}
21 ActivityStackSupervisor的startSpecificActivityLocked
,因为应用程序已经运行,会调用ActivityStackSupervisor的realStartActivityLocked
// Is this activity's application already running?
ProcessRecord app = mService.getProcessRecordLocked(r.processName,
r.info.applicationInfo.uid, true);
r.task.stack.setLaunchTime(r);
if (app != null && app.thread != null) {
try {
if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
|| !"android".equals(r.info.packageName)) {
// Don't add this if it is a platform component 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.versionCode,
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.
}
22 ActivityStackSupervisor的realStartActivityLocked
会调用ActivityThread的ApplicationThread的scheduleLaunchActivity
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
r.icicle, r.persistentState, results, newIntents, !andResume,
23 ApplicationThread的scheduleLaunchActivity
最终会调用ActivityThread的handleLaunchActivity
24 ActivityThread的handleLaunchActivity
会调用自身的performLaunchActivity
Activity a = performLaunchActivity(r, customIntent);
25 ActivityThread的performLaunchActivity
会调用Instrumentation的callActivityOnCreate
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
26 Instrumentation的callActivityOnCreate
会调用Activity的performCreate
activity.performCreate(icicle);
27 Activity的performCreate
会调用自身的onCreate,至此Activity就onCreateonCreate(icicle);
总结
- 和Launcher启动应用程序区别在于应用已启动,在21步的时候,不用新建PID和运行ActivityThread,而是直接启动Activity。
网友评论