Stack structure changes of Android 4.4

Source: Internet
Author: User

Stack structure changes of Android 4.4

We know that the form of activity in AMS is ActivityRecord, the form of task in AMS is TaskRecord, and the management form of process in AMS is ProcessRecord.

Let's take a look at versions earlier than 4.4:
In versions earlier than Android 4.4, the AMS management Task manages all the activities through an ArrayList mHistory:


Conclusion:
(1) All ActivityRecord will be stored in mHistory for management;
(2) Each ActivityRecord corresponds to a TaskRecord with the same TaskRecord
ActivityRecord will be in a continuous position in mHistory;
(3) the Activity of the same TaskRecord may be in different processes.
The process is irrelevant to the task;
(4) TaskRecord and ProcessRecord are not associated.


The management method of version 4.4 has changed:

4.4


4.2


It can be found that 4.4 has added an ActivityStackSupervisor class to help manage TaskStack. You can see from the source code:

/** The stack containing the launcher app */private ActivityStack mHomeStack;/** The non-home stack currently receiving input or launching the nextactivity. Ifhome is* in front then mHomeStack overrides mFocusedStack.* DO NOT ACCESS DIRECTLY - It may be null, use getFocusedStack() */private ActivityStack mFocusedStack;/** All the non-launcher stacks */private ArrayList mStacks = new ArrayList();private static final int STACK_STATE_HOME_IN_FRONT = 0;private static final int STACK_STATE_HOME_TO_BACK = 1;private static final int STACK_STATE_HOME_IN_BACK = 2;private static final int STACK_STATE_HOME_TO_FRONT = 3;private int mStackState = STACK_STATE_HOME_IN_FRONT;

From the annotations, we can see that in Android4.4, we do not use the original mHistory to manage all the activities, but manage them in layers:
In ActivityStackSupervisor, mStacks only contains two stacks: mHomeStack and mFocusStack. Only Launcher tasks are saved in mHomeStack.
In mFocusStack. For Task operations, AMS uses mStackSupervisor. For Acitivity operations, AMS uses ActivityStack.


Conclusion:
(1) At the top of the management layer is an ActivityStack array mStacks, which is used to manage all activitystacks.
(2) there are only two activitystacks in the system. One is mHomeStack, which is used to save the Launcher Activity, and the other is mFocusedStack, which is used to save the Activity of apps other than Launcher.
Ps: The survey found that the task management interface Recent displayed by long-pressed home will also be saved in mHomeStack.
(3) there are only two stacks in the mStacks array, but I don't know why to use a List to manage these two elements.
(4) Each ActivityStack can have multiple taskrecords. These TaskRecord files are stored in ActivityStack. java: ArrayList In mTaskHistory.
(5) In TaskRecord, ArrayList mActivities are included to store information about all the activities in the Task. ActivityStack is included to record the stack to which the Task belongs. int is included.
NumActivities, used to record the number of activities in the current Task.
(6) Based on the above analysis, we can find an Activity in layers: first find the corresponding stack, then find the Task in the stack, and then find the Activity in the Task.

Note that:

void removeTask(TaskRecord task) {mWindowManager.removeTask(task.taskId);final ActivityStack stack = task.stack;final ActivityRecord r = stack.mResumedActivity;if (r != null && r.task == task) {stack.mResumedActivity = null;}if (stack.removeTask(task) && !stack.isHomeStack()) {if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack " + stack);mStacks.remove(stack);final int stackId = stack.mStackId;final int nextStackId = mWindowManager.removeStack(stackId);// TODO: Perhaps we need to let the ActivityManager determine the next focus...if (mFocusedStack == null || mFocusedStack.mStackId == stackId) {// If this is the last app stack, set mFocusedStack to null.mFocusedStack = nextStackId == HOME_STACK_ID ? null :getStack(nextStackId);}}}ActivityStack getLastStack() {switch (mStackState) {case STACK_STATE_HOME_IN_FRONT:case STACK_STATE_HOME_TO_BACK:return mHomeStack;case STACK_STATE_HOME_TO_FRONT:case STACK_STATE_HOME_IN_BACK:default:return mFocusedStack;}}

The source code seems to be switching back and forth between the two stacks. The next FocusedStack will be determined during the switching, but a comment in the source code // TODO: perhaps we need to let the ActivityManager determine the next focus... this is incredible. It seems that how to obtain the next focus is controlled in AMS. Therefore, developers need to pay attention to the android: launchMode = "singleInstance"
Startup Mode, because the scheduling mode in this startup mode in 4.4 is different from that in 4.2 (the reason for the difference should be in ActivityManager, and the specific reason is still under investigation ...), If you want to use it, do a good job of testing.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.