Activity的代表ActivityRecord

2024-05-15 20:48

本文主要是介绍Activity的代表ActivityRecord,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ActivityRecord 是Activity的标识,与每个Activity是一一对应的,
下面我们来介绍他的创建过程
这个既然是代表Activity,那么肯定是在启动Activity的时候,来创建的。
Step 1 :ActivityStackSupervisor.java
final 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);
}

    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)
然后就会传入到我们启动Activity的过程,
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.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<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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/992880

相关文章

LeakCanary测试app内存泄露+registerActivityLifecycleCallbacks管理Activity的生命周期

public class MyApplication extends Application {private String tag = "MyApplication";private static Stack<Activity> activityStack;//检测内存泄露private RefWatcher refWatcher;@Overridepublic void onCreate()

android studio怎么修改创建的Activity的模板?

1.找到android studio的安装目录,如下图,找到对应文件,修改EmptyActivity的模板。 2.修改成如下: 3.新建的EmptyActivity的默认代码如下:

Activity转屏重建之 Activity.onConfigurationChanged

偶尔也会遇到由于转屏引起的一些问题。 有些时候,并不希望由于转屏使得Activity取重建。 再如键盘消失后的重建。 下面以一个demo为例子,小小总结一下用法。 如果想在转屏后,屏幕上立马打印出当前处于什么横竖屏状态 1.都知道有个属性android:configChanges可以用来定义什么情况下可以使得Activity不会restart。 android:configC

查看当前正在运行的Activity列表

1、在终端中输入, 注意adb如果没有配置到系统环境变量里,则需要cd到它所在的文件夹下 “adb shell dumpsys activity” 2、出来的结果比较多,直接搜索"Running" ,找到 Running activities (most recent first):       TaskRecord{4069c020 #4 A shy.luo.ta

Activity被回收导致其内部的Fragment

方式一:   MainActivity重写onSaveInstanceState方法,将super.onSaveInstanceState(outState);注释掉,让其不再保存Fragment的状态,达到其随着MainActivity一起被回收的效果! 方式二: @Overrideprotected void onSaveInstanceState(Bundle outSta

OEM Activity Summary AAS 不记录后台进程 比如 rman

OEM中rman 备份认为是后台进程,所以在Activity Summary未显示 ,AAS 可以显示 去掉后台进程rman 一个也不显示!

Unable to instantiate activity

报的错, 04-25 20:56:13.629: E/Trace(1790): error opening trace file: No such file or directory (2) 04-25 20:56:13.653: E/AndroidRuntime(1790): FATAL EXCEPTION: main 04-25 20:56:13.653: E/AndroidRunt

Activity状态保存【onSaveInstanceState和onRestoreInstanceState】

Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用:   Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate()、onPause()

Android - 总结Activity与Fragment打开的时候获取组件的大小

Activity中获取 组件大小        代码: @Overridepublic void onWindowFocusChanged(boolean hasFocus) {// TODO Auto-generated method stubsuper.onWindowFocusChanged(hasFocus);Log.d("TAG", "A-button1-->"+button1);L

Activity生命周期 与 重建

每一个Android应用程序在运行时,对于底层的Linux Kernel而言都是一个单独的进程,但是对于Android系统而言,因为局限于手机画面的大小与使用的考虑,不能把每一个运行中的应用程序窗口都显示出来。   所以通常手机系统的界面一次仅显示一个应用程序窗口,Android使用了Activity的概念来表示界面。   运行中的应用程序分为五大类,分别是:     前景模式