网上关于在Launcher上第一次启动app的过程分析已经很多了,今天吃饭的时候在思考一个问题:我在Launcher上打开淘宝,切换到购物车,按Home键回到Launcher,再次点开淘宝的时候仍然是购物车页面。按说Launcher每次点开app都是启动同一个activity(ACTION_MAIN , CATEGORY_LAUNCHER),那么这步转换是在哪里完成的呢?
回来翻了翻代码,找到了答案:(有点长,后面会一步一步分析)
final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
boolean doResume, Bundle options, TaskRecord inTask) {
... ...
boolean addingToTask = false;
TaskRecord reuseTask = null;
... ...
if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
(launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
if (inTask == null && r.resultTo == null) {
ActivityRecord intentActivity = !launchSingleInstance ?
findTaskLocked(r) : findActivityLocked(intent, r.info);
if (intentActivity != null) {
... ...
if (r.task == null) {
r.task = intentActivity.task;
}
if (intentActivity.task.intent == null) {
intentActivity.task.setIntent(r);
}
targetStack = intentActivity.task.stack;
targetStack.mLastPausedActivity = null;
final ActivityStack focusStack = getFocusedStack();
ActivityRecord curTop = (focusStack == null)
? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
boolean movedToFront = false;
if (curTop != null && (curTop.task != intentActivity.task ||
curTop.task != focusStack.topTask())) {
r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
if (sourceRecord == null || (sourceStack.topActivity() != null &&
sourceStack.topActivity().task == sourceRecord.task)) {
... ...
movedHome = true;
targetStack.moveTaskToFrontLocked(intentActivity.task,
noAnimation, options, r.appTimeTracker,
"bringingFoundTaskToFront");
movedToFront = true;
... ...
}
... ...
}
... ...
if (!addingToTask &&am