Android Development---Activity launchmode, in-depth understanding of Android task stacks

Source: Internet
Author: User

1,standard:

1. Standard mode, each start creates a new activity, whether or not already exists in the task stack. There can be multiple instances in a task stack, each of which can belong to a different task stack. In this mode who initiates this activity, the current activity is present in the task stack of the activity that initiated it. For example A's task stack is a, now a starts B (Standard mode). There are two activity instances in task stack A at this point, indicating the relationship between the stack and activity:

2. Below we simulate the process. We created 2 activity,mainactivity for a,secondactivity B, and we started with the A b,a,b standard mode. We're using the ADB shell Dumpsys activity

You can see a, and B. In a task stack. The task stack is 52916AAC. The top of the stack is secondyactivity B. If we continue to start B through B.

We can see that. There are three activity,a->b->b within a task stack, even if the activity is created within the stack.

If.

3. Note: If the mode is Standard mode, if we use ApplicationContext to start secondactivity the following error will be reported:

This error can be explained at this time, the non-ACTIVITY type of the context such as ApplicationContext is not the so-called stack, so this error occurs, the workaround is to start the ACTIVITY to add Flag_activity_new_task Flag bit, so you can create a task stack.


2.SingleTop

Stack top multiplexing mode,

1) If the activity is already at the top of the stack, then he is not recreated, but will call its Onnewintent method, which allows us to obtain the requested information.

2) If the activity is not on the top of the stack, the activity will be recreated, such as the on-stack ABCD four activity.a on the bottom and d at the top of the stack. At this point, if you start D,d again, the startup mode is Singletop. Then D is not re-created. If D is standard mode, activity D is recreated. In this case the stack is ABCDD.



3.SingleTask

In-stack multiplexing mode, where the activity is present in the stack, no matter how much you start the activity will not be recreated. As with Singletop, the Onnewintent method is called. How is the system handled?

1) The system first seeks for the presence of the stack needed for activity.

(1) If there is a stack required for activity to determine if there is the same activity instance exists in the stack, it will not be created if it exists. And move the activity to the top of the stack (a. At the top of the stack is not processed, if not at the top of the stack, perform cleartop operations to remove all the active activity from the stack). If the stack does not exist, it is created and then into the stack.

(2) If the activity requires a stack that does not exist, the stack is created and the activity is created into the stack, at which point he is already at the top of the stack.

2) For example:

(1) Assuming that the current task stack is Task1, there is an internal ABC three instances (bottom-----top), this time D with Singletask start, the required task stack is

Task2. At this point, the system discovers that Task2, as well as instances of D, do not exist, and the TASK2 is created and activity D is Task2 into the stack.

(2) In addition, if the stack requested by D is Task1, because Task1 already exists, the system then determines whether there is a D instance in the stack, such as (1) does not exist D this time system

The activity D is incorporated into the stack operation. At this point the interior of the Task1 is ABCD

(3) If D requested the task stack is Task1. And at this time the TASK1 exists in the stack is ADCB, according to the principle of the stack reuse, at this time D will not be created, the system will cut D

Switch to the top of the stack and call the Onnewintent method. At the same time the default exists Cleartop so the CB will be in turn out the stack, when D came to the top of the stack. Last Task1.

The situation in the stack is AD.


Supplementary description The task stack can have multiple, at this time divided into the foreground task stack and the background task stack singletask mode, for example, the current activity is visible, the stack is the foreground stack

The reverse is the background stack. such as activity1,2 in the stack is the foreground stack, activityy,x is the background task stack. At this point, if you start Y, the entire background stack will be switched to the foreground together, if the start X is different. Will go straight to the third step.

Under the simulation process, mainactivity, secondactivity,thirdactivity all start with Singletask, the first one is in Com.ysj.task0, the second third is

Com.ysj.task1. We use the Taskaffinity property to specify the task stack for Singletask mode activity.


We see whether the fourth step results are consistent with the analysis through the ADB shell dumpsys activity command.


As shown, the upper com.sdy.task is the foreground stack and the elements in the stack are thirdactivity, with secondactivity, and thirdactivity on top of the stack. Background stack

Com.sdy.task0. Only Mainactivity.

Then we simulate the COM.SDY.TASK1 switch to the background stack, com.sdy.task0 switch to the foreground, we then proceed to the fifth step above, then we jump to

Background stack of thirdactivity, and secondactivity look at two different situations, how the Android system is handled.

4.SingleInstance, Singleton mode. It has all the attributes that the Singletask has. In addition, the activity can be located in the task stack alone. For example, when a is started in singleinstance mode. The system creates a task for it. Then A is located in the task station alone, and subsequent requests do not create a new instance of a, unless the task stack is destroyed. At the same time if a in singleinstance mode start, and then use a to start the standard mode of the b,b will not be a stack, will be re-create a stack. So singleinstance is a separate task stack.




Android Development---Activity launchmode, in-depth understanding of Android task stacks

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.