Activity栈管理 有助于理解Activity启动流程
Activity在AMS 中存在的形式为ActivityRecord;
AMS以Task的方式管理Activity,Task在AMS存在的形式为TaskRecord
TaskRecord在AMS中依靠ActivityStack去管理
ActivityStack同样有管理者,ActivityStackSupervisor负责管理ActivityStack;
主要涉及四个类
-
ActivityRecord -
存在历史栈的一个实例,代表一个Activity。 -
TaskRecord -
真正的Activity栈,内部维护一个ArrayList -
ActivityStack 栈的结构类 -
并不是一个Activity栈,严格意义上来说TaskRecord才是Activity栈,这个类负责管理各个TaskRecord,内部维护了一个ArrayList + -
ActivityStackSupervisor -
内部持有一个ActivityStack,而ActivityStack内部也持有ActivityStackSupervisor,相当于ActivityStack的辅助管理类
ActivityRecord
ActivityRecord是Activity在AMS中的存在形式,ActivityRecord保存了Activity的信息。
final class ActivityRecord {
TaskRecord task;
int theme;
int launchMode;
...
}
TaskRecord
AMS以Task的方式在管理Activity,Android把用户一次操作相关的Activity按照先后顺序保存在一个Task中,这个Task在AMS中的存在形式就是TaskRecord;
TaskRecord中的mActivities是一个栈,它的作用是以栈的方式组织管理Activity。
final class TaskRecord {
...........
final int taskId;
int mAffiliatedTaskId;
String affinity;
Intent intent;
int mLockTaskMode;
final ArrayList<ActivityRecord> mActivities;
ActivityStack stack;
private Bitmap mLastThumbnail;
private final File mLastThumbnailFile;
final ActivityManagerService mService;
..........
}
ActivityStack
TaskRecord的管理类
final class ActivityStack {
private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
private final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
......
final int mStackId;
final ActivityContainer mActivityContainer;
ArrayList<ActivityStack> mStacks;
int mDisplayId;
......
final ActivityStackSupervisor mStackSupervisor;
ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer, RecentTasks recentTasks) {
mStackSupervisor = activityContainer.getOuter();
...
}
}
ActivityStackSupervisor
ActivityStackSupervisor 用于管理ActivityStack;ActivityStackSupervisor为AMS提供管理方法;管理着系统中的三个ActivityStack;
public final class ActivityStackSupervisor {
private ActivityStack mHomeStack;
private ActivityStack mFocusedStack;
private ActivityStack mLastFocusedStack;
final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
new ArrayList<IActivityManager.WaitResult>();
final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
new ArrayList<IActivityManager.WaitResult>();
final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
......
private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
private final SparseArray<ActivityDisplay> mActivityDisplays =
new SparseArray<ActivityDisplay>();
}
ActivityDisplay
是ActivityContainer内部类
可以理解成为一个屏幕,保存着该屏幕的ActivityStack的数组mStacks,并对其进行管理操作。同时也保存了该屏幕下单特殊ActivityStatck,如mHomeStack、mRecentsStack、mPinnedStack、mSplitScreenPrimaryStack。
|