根Activity的启动过程（API28）

Launcher--AMS

publicclassLauncherextendsBaseDraggingActivityimplementsLauncherExterns,
LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate{
...
publicbooleanstartActivitySafely(View v, Intent intent, ItemInfo item){
boolean success = super.startActivitySafely(v, intent, item);
if (success && v instanceof BubbleTextView) {
// This is set to the view that launched the activity that navigated the user away// from launcher. Since there is no callback for when the activity has finished// launching, enable the press state and keep this reference to reset the press// state when we return to launcher.
BubbleTextView btv = (BubbleTextView) v;
btv.setStayPressed(true);
setOnResumeCallback(btv);
}
return success;
}
...
}
复制代码

/**
* Controller for delegating activity launches.
*
* This class' main objective is to take external activity start requests and prepare them into
* a series of discrete activity launches that can be handled by an {@link ActivityStarter}. It is
* also responsible for handling logic that happens around an activity launch, but doesn't
* necessarily influence the activity start. Examples include power hint management, processing
* through the pending activity list, and recording home activity launches.
*/publicclassActivityStartController{
ActivityStarter obtainStarter(Intent intent, String reason){
return mFactory.obtain().setIntent(intent).setReason(reason);
}
}
复制代码

// Note: This method should only be called from {@link startActivity}.privateintstartActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity){
setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
voiceInteractor);
//处理Activity的启动模式
computeLaunchingTaskFlags();
computeSourceStack();
mIntent.setFlags(mLaunchFlags);
...
int result = START_SUCCESS;
//在前面启动根Activity时，会将Intent的Flag设置为FLAG_ACTIVITY_NEW_TASK，所以这个if语句是成立的if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
&& (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
newTask = true;
//创建新的TaskRecord
result = setTaskFromReuseOrCreateNewTask(taskToAffiliate, topStack);
} elseif (mSourceRecord != null) {
result = setTaskFromSourceRecord();
} elseif (mInTask != null) {
result = setTaskFromInTask();
} else {
// This not being started from an existing activity, and not part of a new task...// just put it in the top task, though these days this case should never happen.
setTaskToCurrentTopOrCreateNewTask();
}
...
//根据Activity的启动模式来判断是直接插入已存在的栈顶还是新的栈
mTargetStack.startActivityLocked(mStartActivity, topFocused, newTask, mKeepCurTransition,
mOptions);
if (mDoResume) {
final ActivityRecord topTaskActivity =
mStartActivity.getTask().topRunningActivityLocked();
if (!mTargetStack.isFocusable()
|| (topTaskActivity != null && topTaskActivity.mTaskOverlay
&& mStartActivity != topTaskActivity)) {
// If the activity is not focusable, we can't resume it, but still would like to// make sure it becomes visible as it starts (this will also trigger entry// animation). An example of this are PIP activities.// Also, we don't want to resume activities in a task that currently has an overlay// as the starting activity just needs to be in the visible paused state until the// over is removed.
mTargetStack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
// Go ahead and tell window manager to execute app transition for this activity// since the app transition will not be triggered through the resume channel.
mService.mWindowManager.executeAppTransition();
} else {
// If the target stack was not previously focusable (previous top running activity// on that stack was not visible) then any prior calls to move the stack to the// will not update the focused stack. If starting the new activity now allows the// task stack to be focusable, then ensure that we now update the focused stack// accordingly.if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {
mTargetStack.moveToFront("startActivityUnchecked");
}
//关键地方
mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
mOptions);
}
} elseif (mStartActivity != null) {
mSupervisor.mRecentTasks.add(mStartActivity.getTask());
}
mSupervisor.updateUserStackLocked(mStartActivity.userId, mTargetStack);
mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(), preferredWindowingMode,
preferredLaunchDisplayId, mTargetStack);
return START_SUCCESS;
}
复制代码

privatebooleanresumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options){
if (!mService.mBooting && !mService.mBooted) {
// Not ready yet!returnfalse;
}
// Find the next top-most activity to resume in this stack that is not finishing and is// focusable. If it is not focusable, we will fall into the case below to resume the// top activity in the next focusable task.final ActivityRecord next = topRunningActivityLocked(true/* focusableOnly */);
......
mStackSupervisor.startSpecificActivityLocked(next, true, false); //注释1
......
复制代码