ActivityManagerService数据结构Activity栈管理(二)
Activity在AMS 中存在的形式为ActivityRecord;
AMS以Task的方式管理Activity,Task在AMS存在的形式为TaskRecord;TaskRecord中的mActivities用栈的方式管理ActivityRecord;
TaskRecord在AMS中依靠ActivityStack去管理,从命名来看,ActivityStack更像Activity栈,但是ActivityStack并不是Activity 栈,而是负责管理TaskRecord的类,android系统中有三种ActivityStack(mHomeStack,mFocusedStack,mLastFocusedStack);
ActivityStack同样有管理者,ActivityStackSupervisor负责管理ActivityStack;
每一个Activity和TaskRecord 都是属于某个进程,所以进程还需要在AMS有存在形式,进程在AMS中存在的形式就是ProcessRecord;
一、主要涉及4个类
1) ActivityRecord
源码注释:An entry in the history stack, representing an activity.
翻译一下:存在历史栈的一个实例,代表一个Activity。
2) TaskRecord
Activity栈,内部维护一个ArrayList<ActivityRecord>
3) ActivityStack
并不是一个Activity栈,真正意义上的Activity栈是TaskRecord,这个类是负责管理各个Activity栈,内部维护一个ArrayList<TaskRecord>
4) ActivityStackSupervisor
内部持有一个ActivityStack,而ActivityStack内部也持有ActivityStackSupervisor,相当于ActivityStack的辅助管理类
ActivityRecord
ActivityRecord是Activity在AMS中的存在形式,ActivityRecord保存了Activity的信息。
final class ActivityRecord {
TaskRecord task; // the task this is in.
final IApplicationToken.Stub appToken;
final int userId;
int theme;
int launchMode;
...
}
成员变量task表示自己所在的TaskRecord,这样要找到自己所在的TaskRecord就不必遍历查找了。
TaskRecord
final class TaskRecord {
...........
final int taskId; // Unique identifier for this task.
int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
// 是指root activity的affinity,即该Task中第一个Activity; 可以理解为当前task的name;
String affinity; // The affinity name for this task, or null; may change identity.
// 启动这个task的intent
Intent intent; // The original intent that started the task.
long firstActiveTime; // First time this task was active.
long lastActiveTime; // Last time this task was active, including sleep.
boolean inRecents; // Actually in the recents list?
boolean isAvailable; // Is the activity available to be launched?
// task模式
int mLockTaskMode; // Which tasklock mode to launch this task in. One of
// ActivityManager.LOCK_TASK_LAUNCH_MODE_* /** List of all activities in the task arranged in history order */
// 该Task中所有的Activity
final ArrayList<ActivityRecord> mActivities; /** Current stack */
// 管理该Task的ActivityStack
ActivityStack stack; // 最近列表中,可以看到当前Task的缩略图
private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
private final File mLastThumbnailFile; // File containing last thumbnail. final ActivityManagerService mService;
..........
}
ActivityStack
ActivityStack充当TaskRecord的Manager角色;
final class ActivityStack {
private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>(); //ActivityStack中所有的TaskRecord
private final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();//最近活动过的ActivityRecord
......
final int mStackId; //ActivityStack的唯一标识
final ActivityContainer mActivityContainer;
/** The other stacks, in order, on the attached display. Updated at attach/detach time. */
ArrayList<ActivityStack> mStacks; //绑定的ActivityDisplay中的所有ActivityStack
/** The attached Display's unique identifier, or -1 if detached */
int mDisplayId;//绑定的ActivityDisplay的id,默认为Display.DEFAULT_DISPLAY = 0;
......
/** Run all ActivityStacks through this */
final ActivityStackSupervisor mStackSupervisor; //ActivityStack的管理者ActivityStackSupervisor ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer, RecentTasks recentTasks) {
mStackSupervisor = activityContainer.getOuter();
...
}
}
mLRUActivities:一个列表,存储的是ActivityStack中按照最近活动情况运行的所有Activity;
ActivityStackSupervisor
ActivityStackSupervisor 用于管理ActivityStack;ActivityStackSupervisor为AMS提供管理方法;管理着系统中的三个ActivityStack;
public final class ActivityStackSupervisor {/** The stack containing the launcher app. Assumed to always be attached to
* Display.DEFAULT_DISPLAY. */
private ActivityStack mHomeStack; /** The stack currently receiving input or launching the next activity. */
private ActivityStack mFocusedStack; /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
* been resumed. If stacks are changing position this will hold the old stack until the new
* stack becomes resumed after which it will be set to mFocusedStack. */
private ActivityStack mLastFocusedStack;
/** List of activities that are waiting for a new activity to become visible before completing
* whatever operation they are supposed to do. */
final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>(); /** List of processes waiting to find out about the next visible activity. */
final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
new ArrayList<IActivityManager.WaitResult>(); /** List of processes waiting to find out about the next launched activity. */
final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
new ArrayList<IActivityManager.WaitResult>(); /** List of activities that are ready to be stopped, but waiting for the next activity to
* settle down before doing so. */
final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>(); /** List of activities that are ready to be finished, but waiting for the previous activity to
* settle down before doing so. It contains ActivityRecord objects. */
final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>(); /** List of activities that are in the process of going to sleep. */
final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
......
/** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();//以mStackId为key /** Mapping from displayId to display current state */
private final SparseArray<ActivityDisplay> mActivityDisplays =
new SparseArray<ActivityDisplay>();//以displayId为key
}
AMS使用mHomeStack,mFocusedStack来完成系统全部的Activity的管理和调度。其中mHomeStack管理的是Launcher相关的任务,包括Launcher、RecentTask,Keyguad,除了上述以外的任务都归mFocusedStack管理。
class ActivityContainer extends android.app.IActivityContainer.Stub {
final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
final int mStackId;
IActivityContainerCallback mCallback = null;
final ActivityStack mStack; //ActivityContainer用于维护ActivityStack的
ActivityRecord mParentActivity = null;
String mIdString; boolean mVisible = true; /** Display this ActivityStack is currently on. Null if not attached to a Display. */
ActivityDisplay mActivityDisplay; final static int CONTAINER_STATE_HAS_SURFACE = 0;
final static int CONTAINER_STATE_NO_SURFACE = 1;
final static int CONTAINER_STATE_FINISHING = 2;
int mContainerState = CONTAINER_STATE_HAS_SURFACE; ActivityContainer(int stackId) {
synchronized (mService) {
mStackId = stackId;
mStack = new ActivityStack(this);
mIdString = "ActivtyContainer{" + mStackId + "}";
if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
}
} void attachToDisplayLocked(ActivityDisplay activityDisplay) {
//ActivityContainer关联ActivityDisplay,用
if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
+ " to display=" + activityDisplay);
mActivityDisplay = activityDisplay;
mStack.mDisplayId = activityDisplay.mDisplayId;
mStack.mStacks = activityDisplay.mStacks; activityDisplay.attachActivities(mStack);
mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
}
......
------>/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java /** Exactly one of these classes per Display in the system. Capable of holding zero or more
* attached {@link ActivityStack}s */
class ActivityDisplay {
/** Actual Display this object tracks. */
int mDisplayId;
Display mDisplay;
DisplayInfo mDisplayInfo = new DisplayInfo(); /** All of the stacks on this display. Order matters, topmost stack is in front of all other
* stacks, bottommost behind. Accessed directly by ActivityManager package classes */
final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>(); ActivityRecord mVisibleBehindActivity; ActivityDisplay() {
}
// After instantiation, check that mDisplay is not null before using this. The alternative
// is for this to throw an exception if mDisplayManager.getDisplay() returns null.
ActivityDisplay(int displayId) {
final Display display = mDisplayManager.getDisplay(displayId);
if (display == null) {
return;
}
init(display);
}
.......
void attachActivities(ActivityStack stack) {
if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
+ mDisplayId);
mStacks.add(stack);
}
......
ActivityStackSupervisor对ActivityRecord的管理过程如下:
ActivityStackSupervisor.mActivityDisplays
-> ActivityDisplay.mStacks
-> ActivityStack.mTaskHistory
-> TaskRecord.mActivities
-> ActivityRecord
二、场景解析
1、从桌面第一次启动App
startActivityLocked里构造一个ActivityRecord
新建一个TaskRecord,并存入mTaskHistory
ActivityRecord存入mActivities
final int startActivityUncheckedLocked(...) {
final int startActivityUncheckedLocked(...) {
if (reuseTask == null) {
r.setTask(targetStack.createTaskRecord(...);
...
targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
...
}
}
1.TaskRecord存入mTaskHistory
TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
boolean toTop) {
TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
voiceInteractor);
addTask(task, toTop, false);
return task;
} void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
task.stack = this;
if (toTop) {
insertTaskAtTop(task, null);
} else {
mTaskHistory.add(0, task);
updateTaskMovement(task, false);
}
...
} private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
...
mTaskHistory.add(taskNdx, task);
updateTaskMovement(task, true);
}
2.ActivityRecord存入mActivities
final void startActivityLocked(ActivityRecord r, boolean newTask, ...) {
...
task = mTaskHistory.get(taskNdx);
...
task.addActivityToTop(r);
}
void addActivityToTop(ActivityRecord r) {
addActivityAtIndex(mActivities.size(), r);
}
void addActivityAtIndex(int index, ActivityRecord r) {
...
mActivities.add(index, r);
...
}
ActivityManagerService数据结构Activity栈管理(二)的更多相关文章
- Android解析ActivityManagerService(二)ActivityTask和Activity栈管理
前言 关于AMS,原计划是只写一篇文章来介绍,但是AMS功能繁多,一篇文章的篇幅远远不够.这一篇我们接着来学习与AMS相关的ActivityTask和Activity栈管理. 1.ActivitySt ...
- activity栈管理的3种方式
一.背景 在android开发过程最经常使用的组件非activity莫属. 通过分析activity的各种跳转,执行同学能够分析用户的各种行为.更重要的一点是在做插件化的过程中,我们经常会对activ ...
- android的activity栈管理
在进行BlackBerry程序开发的时候,BlackBerry提供了一个管理Screen的栈,用来从任何地方来关闭位于最上一层的Screen,使用UiApplication.getUiApplicat ...
- 【转】Android 之ActivityThead、ActivityManagerService 与activity的管理和创建
在android中,Activity是四大组件中比较重要的一个(当然其他的也比较重要),那么android中是怎样管理这些activity的?应用的进程和主线程是怎么创建的,应用的消息循环又是在什么时 ...
- 如何在程序退出的时候清除activity栈
在公司里接手了一个后期的项目,由于项目前期对activity栈管理的不够谨慎,所以导致了在某些情况下程序退出的时候没有将activity栈清除掉.在网上找到的无非就是那几种例子,都不是最好的解决办法. ...
- activity的启动模式和栈管理
在学习Android的过程中,Intent是我们最常用Android用于进程内或进程间通信的机制,其底层的通信是以Binder机制实现的,在物理层则是通过共享内存的方式实现的. Intent ...
- ActivityJump+ActivityManager【Activity之间的跳转和Activity任务栈管理】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装Activity跳转的方法以及实现Activity任务栈管理. 效果图 代码分析 ActivityJump:封装Activi ...
- Activity 生命周期及其栈管理方式
Activity 生命周期 Android 系统用栈的形式管理 Activity , 当新的 Activity 被创建是, 会被放置到栈顶, 这个 Activity 会进入到运行状态, 而前一个 Ac ...
- Activity栈与任务管理探究1——栈与任务的概述
Activity栈与任务管理探究1--栈与任务的概述 内容概览 1. 前言 2. Activity中的Stack 3. Activity中的Task 4. Activity栈与任务管理基本原则 1. ...
随机推荐
- static在C和C++里各代表什么含义
转自:http://blog.csdn.net/wanglongfei_hust/article/details/10011503 static关键字有三种使用方式,其中前两种只指在C语言中使用,第三 ...
- .NET基础 (05)内存管理和垃圾回收
内存管理和垃圾回收1 简述.NET中堆栈和堆的特点和差异2 执行string abc="aaa"+"bbb"+"ccc"共分配了多少内存3 ...
- 创建Jutil (单元测试)
如何创建JUtil 这里拿Dynamic项目来演示,首先创建一个Dynamic项目,起名,点next, 继续点next, 将web.xml文件勾选,finish, 接下来在Java Resources ...
- 【转载】python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了: count = len(o ...
- MongoDB默认配置
mongodb使用了yaml格式定义的配置文件(http://www.yaml.org/) 默认为: # mongod.conf #where to log logpath=/var/log/mong ...
- pagecontrol
PageControl组件位于组件板的Win32页中,该组件用于 实现窗体上多页面技术,每个页面上均能添加若干控件.程序运行时,单击页面标签就可以在多页之间切换.1.建立多页 用鼠标右键单击PageC ...
- 纸壳CMS 2.3,正式加入商城功能
纸壳CMS发布了2.3版本,主要是添加了商城功能,强化产品功能.让您的网站轻松实现电子商务. 有关2.3版本的更多信息,请查看以下链接: https://github.com/SeriaWei/ZKE ...
- ES6—— iterator和for-of循环
Iterator 遍历器的作用:为各种数据结构,提供一个同意的,简便的访问接口.是的数据结构的成员能够按某种次序排列.ES6 新增了遍历命令 for...of 循环,Iterator接口主要供 for ...
- fhq treap——简单又好写的数据结构
今天上午学了一下fhq treap感觉真的很好用啊qwq 变量名解释: \(size[i]\)表示以该节点为根的子树大小 \(fix[i]\)表示随机权值 \(val[i]\)表示该节点的值 \(ch ...
- C# Winform下一个热插拔的MIS/MRP/ERP框架15(窗体基类场景1)
最基础的窗体基类其实是通过应用场景反推的结构. 以下是场景一: 单表应用,普通的数据,比如单位/颜色/特殊字典等使用者少的,无需过多控制的可以使用一个数据表格来管理. 和Excel表格差不多,批量修改 ...