本文主要是介绍Activity的代表ActivityRecord,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ActivityRecord 是Activity的标识,与每个Activity是一一对应的,下面我们来介绍他的创建过程这个既然是代表Activity,那么肯定是在启动Activity的时候,来创建的。Step 1 :ActivityStackSupervisor.javafinal int startActivityLocked(IApplicationThread caller,Intent intent, String resolvedType, ActivityInfo aInfo,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,IBinder resultTo, String resultWho, int requestCode,int callingPid, int callingUid, String callingPackage,int realCallingPid, int realCallingUid, int startFlags, Bundle options,boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,TaskRecord inTask) {ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,requestCode, componentSpecified, this, container, options);err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,startFlags, true, options, inTask); }
然后就会传入到我们启动Activity的过程,ActivityRecord( ActivityManagerService _service, ProcessRecord _caller, //调用者的进程,与aInfo一起来决定当前Activity的应用包名int _launchedFromUid, //启动Activity的UidString _launchedFromPackage, //启动Activity的包名Intent _intent, //启动的IntentString _resolvedType, //调用的包名ActivityInfo aInfo, //Activity的信息Configuration _configuration, //Activity的配置信息ActivityRecord _resultTo, //parent Activity的信息String _resultWho, //parent Activity的包名int _reqCode, //startActivityForResult中的RequestCodeboolean _componentSpecified, //boolean componentSpecified = intent.getComponent() != null;ActivityStackSupervisor supervisor,ActivityContainer container, //大多数为null,一些特殊的启动方式会有值.Bundle options)
Step 2 :ActivityStackSupervisor.java<pre style="border: 0px; margin-top: 0px; margin-bottom: 0px; background-color: rgb(255, 255, 255);"><pre name="code" class="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.javaint 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<ActivityStack> 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
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); <span style="white-space:pre"> </span>//对于新栈,直接插入就好,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);<span style="white-space:pre"> </span>//之前都是针对栈的操作。这里才是将当前的Activity移植栈顶。r.putInHistory();<span style="white-space:pre"> </span>//这里就是对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();}ActivityOptions.abort(options);return;}break;} else if (task.numFullscreen > 0) {startIt = false;}}}
这篇关于Activity的代表ActivityRecord的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!