Stack structure changes for Android 4.4

Source: Internet
Author: User

We know that the activity in AMs in the form of Activityrecord,task in AMs in the form of Taskrecord, process in AMS management form is Processrecord.

Let's look at the previous version of 4.4:
Prior to android4.4, the AMS management Task was to manage all the activity through a ArrayList mhistory:


The conclusions are as follows:
(1) All Activityrecord will be stored in mhistory management;
(2) Each activityrecord will correspond to a taskrecord, and have the same taskrecord
Activityrecord in the mhistory will be in a continuous position;
(3) The activity of the same taskrecord may be in separate processes, each activity
Process is not related to task;
(4) Taskrecord and Processrecord are not connected.


The management of the 4.4 version has changed:

4.4


4.2


Can be found, 4.4 added a Activitystacksupervisor class to assist the management of Taskstack, see the Source:

/** the stack containing the Launcher app */private Activitystack mhomestack;/** the Non-home stack currently receiving in Put or launching the nextactivity. Ifhome is* in front then Mhomestack overrides mfocusedstack.* does not ACCESS directly-it May is null, use Getfocusedstack () */private Activitystack mfocusedstack;/** all the Non-launcher stacks */private arraylist<activitystack> Mstacks = new arraylist<activitystack> ();p rivate static final int stack_state_home_in_front = 0;private static fina l int stack_state_home_to_back = 1;private static final int stack_state_home_in_back = 2;private static final int Stack_st Ate_home_to_front = 3;private int mstackstate = Stack_state_home_in_front;

        As can be seen from the annotations, in Android4.4, the original mhistory is not used to manage all the activity, but is managed hierarchically:
Only two stacks are included in the Activitystacksupervisor mstacks Mhomestack and Mfocusstack. Only the Launcher task is saved in the Mhomestack, and the other tasks are put into
The Mfocusstack. For Task operations, AMS uses Mstacksupervisor. For acitivity operation, AMS uses Activitystack.


The conclusions are as follows:
(1) The top of the management hierarchy is an array of type Activitystack mstacks, which is used to manage all activitystack.
(2) There are only two activitystack in the system, one is Mhomestack, which is used to save Launcher activity, and the other is Mfocusedstack, which is used to save the activity of the non-Launcher App.
PS: The survey found that the task management interface recent, long press home, will also be saved in Mhomestack.
(3) Mstacks array, only the above two stacks, but do not know why to use a List to manage the two elements.
(4) In each activitystack, you can have more than one taskrecord. These taskrecord are stored in the activitystack.java:arraylist<taskrecord> mtaskhistory.
(5) In Taskrecord, contains arraylist<activityrecord> mactivities, which holds information about all the Activity in the task, including the Activitystack stack, Used to record which stack it belongs to; contains int
Numactivities, used to record the number of Activity in the current Task.
(6) The above analysis shows that in order to find an activity, you need to find a hierarchy: first find the corresponding stack, then find the task in the stack, and then find the activity in the task.

It is important to 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 | | mfocu Sedstack.mstackid = = Stackid) {//If This was 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;}

        Source code seems to be in these two stacks to switch back and forth, when switching will determine the next focusedstack, but the source of a comment//todo:perhaps We need to let the Activitymanager determine th E Next Focus ... It's a bit more bizarre than that. It seems that the specific how to get the next focus is controlled in AMS. So, developers need to pay attention to using android:launchmode= "singleinstance" in 4.4 development.
Starting mode, because 4.4 under this startup mode of scheduling and 4.2 differences (the reason for the difference should be in the Activitymanager, the specific reason is still in the investigation ...) If you want to use it, be sure to do the testing.




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.