finalintstartActivityMayWait(IApplicationThreadcaller,intcallingUid,StringcallingPackage,Intentintent,StringresolvedType,IVoiceInteractionSessionvoiceSession,IVoiceInteractorvoiceInteractor,IBinderresultTo,StringresultWho,intrequestCode,intstartFlags,ProfilerInfoprofilerInfo,WaitResultoutResult,Configurationconfig,Bundleoptions,booleanignoreTargetSecurity,intuserId,IActivityContaineriContainer,TaskRecordinTask){// 省略部分代码// Collect information about the target of the Intent.// 通过 PackageManagerService 解析 Intent 参数内容,获得更多信息,保存到 ActivityInfo 类中ActivityInfoaInfo=resolveActivity(intent,resolvedType,startFlags,profilerInfo,userId);// 省略部分关于 aInfo 代码intres=startActivityLocked(caller,intent,resolvedType,aInfo,voiceSession,voiceInteractor,resultTo,resultWho,requestCode,callingPid,callingUid,callingPackage,realCallingPid,realCallingUid,startFlags,options,ignoreTargetSecurity,componentSpecified,null,container,inTask);Binder.restoreCallingIdentity(origId);returnres;}}// resolveActivity 函数ActivityInforesolveActivity(Intentintent,StringresolvedType,intstartFlags,ProfilerInfoprofilerInfo,intuserId){// Collect information about the target of the Intent.ActivityInfoaInfo;try{ResolveInforInfo=AppGlobals.getPackageManager().resolveIntent(intent,resolvedType,PackageManager.MATCH_DEFAULT_ONLY|ActivityManagerService.STOCK_PM_FLAGS,userId);aInfo=rInfo!=null?rInfo.activityInfo:null;}catch(RemoteExceptione){aInfo=null;}// 省略部分代码}
finalintstartActivityUncheckedLocked(finalActivityRecordr,ActivityRecordsourceRecord,IVoiceInteractionSessionvoiceSession,IVoiceInteractorvoiceInteractor,intstartFlags,booleandoResume,Bundleoptions,TaskRecordinTask){finalIntentintent=r.intent;finalintcallingUid=r.launchedFromUid;// 得到目标 Activity 组件的启动标志位intlaunchFlags=intent.getFlags();// We'll invoke onUserLeaving before onPause only if the launching// activity did not explicitly state that this is an automated launch.mUserLeaving=(launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION)==0;booleanaddingToTask=false;// 是否将需要启动的 Activity 添加到给定的 task 中TaskRecordreuseTask=null;// // If the caller is not coming from another activity, but has given us an// explicit task into which they would like us to launch the new activity,// then let's see about doing that.// 如果调用者if(sourceRecord==null&&inTask!=null&&inTask.stack!=null){// 省略部分代码// If task is empty, then adopt the interesting intent launch flags in to the// activity being started.if(root==null){finalintflagsOfInterest=Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_DOCUMENT|Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;launchFlags=(launchFlags&~flagsOfInterest)|(baseIntent.getFlags()&flagsOfInterest);intent.setFlags(launchFlags);inTask.setIntent(r);addingToTask=true;}// 省略启动 Activity 已经启动过的情况,主要是将 Activity 移植 Task 栈顶// 启动一个从未启动过的 Activity booleannewTask=false;// 表示在一个新的 Task 中启动 ActivitybooleankeepCurTransition=false;// Should this be considered a new task?// 是否要创建一个新的 Task ,当然是要的if(r.resultTo==null&&inTask==null&&!addingToTask&&(launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK)!=0){newTask=true;targetStack=computeStackFocus(r,newTask);targetStack.moveToFront("startingNewTask");if(reuseTask==null){// reuseTask 为 null,创建一个新的 Task r.setTask(targetStack.createTaskRecord(getNextTaskId(),newTaskInfo!=null?newTaskInfo:r.info,newTaskIntent!=null?newTaskIntent:intent,voiceSession,voiceInteractor,!launchTaskBehind/* toTop */),taskToAffiliate);if(DEBUG_TASKS)Slog.v(TAG_TASKS,"Starting new activity "+r+" in new task "+r.task);}else{r.setTask(reuseTask,taskToAffiliate);}if(isLockTaskModeViolation(r.task)){Slog.e(TAG,"Attempted Lock Task Mode violation r="+r);returnActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;}if(!movedHome){if((launchFlags&(FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_TASK_ON_HOME))==(FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_TASK_ON_HOME)){// Caller wants to appear on home activity, so before starting// their own activity we will bring home to the front.r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);}}}elseif(sourceRecord!=null){// 上面的 if 判断成立,省略代码// 在调用者的 Task 中做操作 ,resumeTopActivityLocked 方法}elseif(inTask!=null){// 上面的 if 判断成立,省略代码// 在指定的 Task 中做操作,}else{// 上面的 if 判断成立,省略代码}mService.grantUriPermissionFromIntentLocked(callingUid,r.packageName,intent,r.getUriPermissionsLocked(),r.userId);if(sourceRecord!=null&&sourceRecord.isRecentsActivity()){r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);}if(newTask){EventLog.writeEvent(EventLogTags.AM_CREATE_TASK,r.userId,r.task.taskId);}ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY,r,r.task);targetStack.mLastPausedActivity=null;// 启动 Activity 组件的下一步targetStack.startActivityLocked(r,newTask,doResume,keepCurTransition,options);if(!launchTaskBehind){// Don't set focus on an activity that's going to the back.mService.setFocusedActivityLocked(r,"startedActivity");}returnActivityManager.START_SUCCESS;}
finalvoidstartActivityLocked(ActivityRecordr,booleannewTask,booleandoResume,booleankeepCurTransition,Bundleoptions){TaskRecordrTask=r.task;finalinttaskId=rTask.taskId;// mLaunchTaskBehind tasks get placed at the back of the task stack.if(!r.mLaunchTaskBehind&&(taskForIdLocked(taskId)==null||newTask)){// Last activity in task had been removed or ActivityManagerService is reusing task.// Insert or replace.// Might not even be in.insertTaskAtTop(rTask,r);mWindowManager.moveTaskToTop(taskId);}if(!newTask){// newTask 为 true ,省略部分代码}// Place a new activity at top of stack, so it is next to interact// with the user.// If we are not placing the new activity frontmost, we do not want// to deliver the onUserLeaving callback to the actual frontmost activity // task 变量为 null,尚未赋值, IF 判断不成立if(task==r.task&&mTaskHistory.indexOf(task)!=(mTaskHistory.size()-1)){mStackSupervisor.mUserLeaving=false;if(DEBUG_USER_LEAVING)Slog.v(TAG_USER_LEAVING,"startActivity() behind front, mUserLeaving=false");}task=r.task;// Slot the activity into the history stack and proceedif(DEBUG_ADD_REMOVE)Slog.i(TAG,"Adding activity "+r+" to stack to task "+task,newRuntimeException("here").fillInStackTrace());task.addActivityToTop(r);task.setFrontOfTask();r.putInHistory();// 省略 Window 添加部分代码if(doResume){mStackSupervisor.resumeTopActivitiesLocked(this,r,options);}}// 将 ActivityRecord 添加到栈的方法voidaddActivityToTop(ActivityRecordr){addActivityAtIndex(mActivities.size(),r);}
finalbooleanresumeTopActivityLocked(ActivityRecordprev,Bundleoptions){if(mStackSupervisor.inResumeTopActivity){// Don't even start recursing.returnfalse;}booleanresult=false;try{// Protect against recursion.mStackSupervisor.inResumeTopActivity=true;if(mService.mLockScreenShown==ActivityManagerService.LOCK_SCREEN_LEAVING){mService.mLockScreenShown=ActivityManagerService.LOCK_SCREEN_HIDDEN;mService.updateSleepIfNeededLocked();}result=resumeTopActivityInnerLocked(prev,options);}finally{mStackSupervisor.inResumeTopActivity=false;}returnresult;}
privatebooleanresumeTopActivityInnerLocked(ActivityRecordprev,Bundleoptions){// Find the first activity that is not finishing.// 找到当前 ActivityRecord 的栈顶,指向了要启动的 Activity 组件。finalActivityRecordnext=topRunningActivityLocked(null);finalTaskRecordprevTask=prev!=null?prev.task:null;if(next==null){进入该分支表示没有要启动的Activity。}// 省略部分代码// If the top activity is the resumed one, nothing to do.// 检查要启动的 Activity 组件是否等于当前被激活的 Activity 组件,如果等于,并且处于 RESUMED 状态,直接返回if(mResumedActivity==next&&next.state==ActivityState.RESUMED&&mStackSupervisor.allResumedActivitiesComplete()){// Make sure we have executed any pending transitions, since there// should be nothing left to do at this point.mWindowManager.executeAppTransition();mNoAnimActivities.clear();ActivityOptions.abort(options);returnfalse;}// If we are sleeping, and there is no resumed activity, and the top// activity is paused, well that is the state we want.// 检查要启动的 Activity 组件是否等于上一次被中止了的 Activity 组件,如果等于,// 并且这时候系统正要进入关机或睡眠状态,则直接退出,启动毫无意义if(mService.isSleepingOrShuttingDown()&&mLastPausedActivity==next&&mStackSupervisor.allPausedActivitiesComplete()){// Make sure we have executed any pending transitions, since there// should be nothing left to do at this point.mWindowManager.executeAppTransition();mNoAnimActivities.clear();ActivityOptions.abort(options);returnfalse;}// If we are currently pausing an activity, then don't do anything until that is done.// 检查系统中止 Activity 组件是否完成,如果没有,则直接返回了,等待所有的 Activity 进入中止状态if(!mStackSupervisor.allPausedActivitiesComplete()){returnfalse;}// We need to start pausing the current activity so the top one can be resumed...// Launcher 组件进入 onPause 状态booleandontWaitForPause=(next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING)!=0;booleanpausing=mStackSupervisor.pauseBackStacks(userLeaving,true,dontWaitForPause);if(mResumedActivity!=null){// mResumedActivity 指向了 Launcher 组件,不为 null ,则中止 Launcher 组件pausing|=startPausingLocked(userLeaving,false,true,dontWaitForPause);}// 省略部分代码if(next.app!=null&&next.app.thread!=null){// 待启动的 Activity 在新的进程中,app 变量为 null }else{// 创建一个新的应用程序进程mStackSupervisor.startSpecificActivityLocked(next,true,true);}}