int _launchedFromUid, //启动Activity的Uid
String _launchedFromPackage, //启动Activity的包名
Intent _intent, //启动的Intent
String _resolvedType, //调用的包名
ActivityInfo aInfo, //Activity的信息
Configuration _configuration, //Activity的配置信息
ActivityRecord _resultTo, //parent Activity的信息
String _resultWho, //parent Activity的包名
int _reqCode, //startActivityForResult中的RequestCode
boolean _componentSpecified, //boolean componentSpecified = intent.getComponent() != null;
ActivityStackSupervisor supervisor,
ActivityContainer container, //大多数为null,一些特殊的启动方式会有值.
Bundle options)
然后就会传入到我们启动Activity的过程,
Step 2 :ActivityStackSupervisor.java
final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags, boolean doResume, Bundle options, TaskRecord inTask) { if (r.resultTo == null && inTask == null && !addingToTask && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { newTask = true; targetStack = adjustStackFocus(r, newTask); //这一步相当于找到Activity所在的栈,下一部是移动的操作 if (!launchTaskBehind) { targetStack.moveToFront("startingNewTask"); //将当前的ActivityStack从栈里移到栈顶。 } if (reuseTask == null) { r.setTask(targetStack.createTaskRecord(getNextTaskId(), //这里是创建一个新的Task,作为要启动的Activity所在的栈,这里有一个唯一的TaskId标识,方法是每次id++查找一遍,如果已经存在则再++查找,感觉这里效率是可以优化的。geNextTaskId()在Step 3中,感兴趣的可以提出优化方法一起讨论一下。 newTaskInfo != null ? newTaskInfo : r.info, newTaskIntent != null ? newTaskIntent : intent, voiceSession, voiceInteractor, !launchTaskBehind /* toTop */), taskToAffiliate); if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " + r.task); } } else if (sourceRecord != null) { ...... //这里过程类似,省略掉 } else if (inTask != null) { ...... } 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. targetStack = adjustStackFocus(r, newTask); targetStack.moveToFront("addingToTopTask"); ActivityRecord prev = targetStack.topActivity(); r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(), //这里则是另一种情况,新启动的Activity与之前的Activity公用一个栈。 r.info, intent, null, null, true), null); mWindowManager.moveTaskToTop(r.task.taskId); if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new guessed " + r.task); } **Step 3 [ActivityStackSupervisor.java](http://androidxref.com/5.1.1_r6/xref/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java)** int getNextTaskId() { do { mCurTaskId++; if (mCurTaskId <= 0) { mCurTaskId = 1; } } while (anyTaskForIdLocked(mCurTaskId) != null); return mCurTaskId; } TaskRecord anyTaskForIdLocked(int id) { int numDisplays = mActivityDisplays.size(); for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) { ArrayList stacks = mActivityDisplays.valueAt(displayNdx).mStacks; for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) { ActivityStack stack = stacks.get(stackNdx); TaskRecord task = stack.taskForIdLocked(id); if (task != null) { return task; } } } // Don't give up! Look in recents. if (DEBUG_RECENTS) Slog.v(TAG, "Looking for task id=" + id + " in recents"); TaskRecord task = mService.recentTaskForIdLocked(id); if (task == null) { if (DEBUG_RECENTS) Slog.d(TAG, "\tDidn't find task id=" + id + " in recents"); return null; } if (!restoreRecentTaskLocked(task)) { if (DEBUG_RECENTS) Slog.w(TAG, "Couldn't restore task id=" + id + " found in recents"); return null; } if (DEBUG_RECENTS) Slog.w(TAG, "Restored task id=" + id + " from in recents"); return task; } **Step 4 : [ActivityStack.java](http://androidxref.com/5.1.1_r6/xref/frameworks/base/services/core/java/com/android/server/am/ActivityStack.java)** final void startActivityLocked(ActivityRecord r, boolean newTask, boolean doResume, boolean keepCurTransition, Bundle options) { TaskRecord rTask = r.task; final int taskId = 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); //对于新栈,直接插入就好, mWindowManager.moveTaskToTop(taskId); } TaskRecord task = null; if (!newTask) { // If starting in an existing task, find where that is... boolean startIt = true; for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { task = mTaskHistory.get(taskNdx); if (task.getTopActivity() == null) { // All activities in task are finishing. continue; } if (task == r.task) { // Here it is! Now, if this is not yet visible to the // user, then just add it without starting; it will // get started when the user navigates back to it. if (!startIt) { if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task " + task, new RuntimeException("here").fillInStackTrace()); task.addActivityToTop(r); //之前都是针对栈的操作。这里才是将当前的Activity移植栈顶。 r.putInHistory(); //这里就是对ActivityRecord的一个标志位的设置 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken, //这里是单独记录AppWindowToken可以把他理解我Activity对应的window的唯一标识,这里就是将Activity与Window挂起来的地方。