Fully understand Android activity startup mode Lauchmode (deep activity and task stack)

Source: Internet
Author: User
Tags home screen

Before the author has talked about the role of Lauchmode, and as far as possible to avoid the concept of the stack using GIF pictures as simple as possible to explain the activity of the start mode, this article once again in depth, a good talk about the various startup modes, How activity and task stacks work.

If the reader is just getting started, it is recommended that you look at the author's previous article first.

Previous post address: http://blog.csdn.net/double2hao/article/details/50929431


Task stack: (I'll copy the explanations in the official documentation here.)

If you have an in-depth knowledge of the task stack, you can look at the official translation:

http://blog.csdn.net/double2hao/article/details/51589363


When the activity starts another activity, the new activity is pushed to the top of the stack, which becomes the focus. The previous Activity remains on the stack but is in a stopped state. When Activity stops, the system maintains the current state of its user interface. When the user presses the Back button, the current activity pops up from the top of the stack (activity is destroyed), while the previous activity resumes execution (restores the previous state of its UI). The activity in the stack is never rearranged, only pushes in and pops up the stack: pushed into the stack by the current Activity startup, and pops up when the user exits with the return button. Therefore, the return stack runs with a "last in, first out" object structure. Figure 1 shows this behavior visually by displaying the progress between activities and the current return stack for each point in time through the timeline.


Figure 1. Shows how each new Activity in the task adds items to the return stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes execution. If the user continues to press RETURN, the corresponding activity in the stack pops up to show the previous activity until the user returns to the home screen (or any Activity that is running at the beginning of the task). When all Activity is removed from the stack, the task ceases to exist.


Four modes of startup:


Standard: Normal mode, which is also the default mode for the system.

In this mode, who initiates the activity, the activity runs in the same stack as the activity on which it was launched.

Example:

1, activity A in the task stack S1, activity b in the task stack S2, then if it is a start the standard mode of C, then C will be in the S1 stack, if B is activated C will be in the S2 stack.


Singletop: Stack top multiplexing mode.

This model is an optimized version of standard. Who is still initiating the activity, the activity is running in the same stack as the activity on which it was launched. The difference is one more judgment.

Example:

1, the current situation in the S1 stack is ABCD, at this time to start activity D, if the boot mode is standard, then the situation in the stack is ABCDD. But if it's singletop mode, the inside of the stack is still ABCD.

Note: Instead of recreating a d after the original D was destroyed, the D that was previously created is used directly.


Singletask: In-stack multiplexing mode.

In this mode, as long as the activity is present in a stack, starting the activity multiple times will not recreate the instance, but instead bring the previously created instance to use. But if there is no instance of the activity, a stack is reopened and the activity is put in.

Singletask also has a cleartop effect by default, which causes all activity on the stack to be stacked on top of all the activity on the reuse.

Example:

1, the current S1 task stack in the case of ABC, at this time to start the station reuse mode activity D, because the original stack does not exist D, then a new task stack S2, and create an instance of D into S2.

2, the current task stack S1 situation is adbc.

If Standard mode is enabled activity D, then the situation in S1 becomes ADBCD.

and activity D, which initiates the reuse mode in the station, is not re-created according to the principle of the reuse of the stack, and the system will re-switch D to the top of the stack. In principle, the situation in the stack should become ABCD, but since singletask default has a cleartop effect, the end result should be AD.


SingleInstance: Single-instance mode.

Words too literally, that is, there will only be one single instance in a task stack. Of course, there is only one instance in the stack, so any activity opened by this instance will be opened in a separate task stack.

Example:

1, the current task stack S1 The situation is ABC(ABC startup mode is Standard or Singletop ), When you start activity D for a single instance, you create a new task stack S2 and put D in. Opening any of the ABC again from Activity D will create a new S3 task stack and put this activity into. At this point there will be three task stacks S1, S2, S3.

2, the current task stack S1 is ABC(A's start mode is Singletask), at this time to start a single instance of activity D, will create a new task stack S2 and put D into. When activity A is opened from Activity D, a in S1 is reused. This means that there are still only two task stacks S1 and S2. However, follow the principle of singleinstance " any activity opened by this instance will be opened in a separate task stack ".

Fully understand Android activity startup mode Lauchmode (deep activity and task stack)

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.