AMS重要的数据结构解析(一):ActivityRecord

err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,

startFlags, true, options, inTask);

}

ActivityRecord( ActivityManagerService _service,

ProcessRecord _caller, //调用者的进程,与aInfo一起来决定当前Activity的应用包名

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](https://docs.qq.com/doc/DSkNLaERkbnFoS0ZF)**





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](https://docs.qq.com/doc/DSkNLaERkbnFoS0ZF)**





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挂起来的地方。







r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,







(r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,







r.userId, r.info.configChanges, task.voiceSession != null,







r.mLaunchTaskBehind);







if (VALIDATE_TOKENS) {







validateAppTokensLocked();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值