前言
ActivityRecord、Task、ActivityStack是和ActivityManager相关的三个很重要的数据结构,只有理解了它们,我们才能更好的理解Activity的任务栈模型。
一、ActivityRecord、Task、ActivityStack对象解析
1、ActivityRecord在启动Activity的时候被创建,主要用来描述一个Activity,其内部记录了Activity的所有信息,下面我们挑部分字段进行介绍:
frameworks/base/services/core/java/com/android/server/am/ActivityRecord.java
final class ActivityRecord extends WindowToken implements WindowManagerService.AppFreezeListener {
...代码省略...
final ActivityTaskManagerService mAtmService;
final ActivityInfo info; // 开发人员在AndroidManifest中配置的Activity节点信息
final String packageName;// Activity所对应的包名
final int launchedFromPid; // 启动Activity的应用进程id
final int launchedFromUid; // 启动Activity的应用用户id
final String launchedFromPackage; // 启动Activity的应用包名
final Intent intent; // 启动当前Activity的Intent
final String processName; // Activity所属的进程名称
final String taskAffinity; //Activity所归属的栈
final boolean stateNotNeeded; // As per ActivityInfo.flags
private int labelRes; // the label information from the package mgr.
private int icon; // Activity的图标所对应的资源id
private int logo; // Activity的logo所对应的资源id
private int theme; // Activity的主题所对应的资源id
private int windowFlags; // 自定义的窗口属性
private Task task; // Activity所属的任务
private long createTime = System.currentTimeMillis();
long lastVisibleTime; // 当前Activity最后设置为可见的时间
long pauseTime; // 当前Activity进入pause的最后时间
long launchTickTime; // base time for launch tick messages
ActivityRecord resultTo; // 谁开启了这个入口, 将会收到回复
final String resultWho; // additional identifier for use by resultTo.
final int requestCode; // 请求码
private State mState; // Activity当前所处的状态
...代码省略...
}
在上面所罗列的ActivityRecord的属性中,有一个类型为Task的task属性,它描述了当前Activity所属的任务栈。
2、Task对象内部也有诸多属性,下面我们挑部分字段进行介绍:
frameworks/base/services/core/java/com/android/server/wm/Task.java
class Task extends TaskFragment {
...代码省略...
final int mTaskId; // 任务栈的唯一标识符
String affinity; // The affinity name for this task, or null; may change identity.
String rootAffinity; // Initial base affinity, or null; does not change from initial root.
String mWindowLayoutAffinity; // Launch param affinity of this task or null. Used when saving
Intent intent; // 启动这个Task的Intent
final ArrayList<ActivityRecord> mExitingActivities = new ArrayList<>();
...代码省略...
}