Use of Activity rollback stack and Activity rollback Stack

Source: Internet
Author: User

Use of Activity rollback stack and Activity rollback Stack

When an APP is started, the system creates a rollback stack (task) for the APP to save all the created Activity of the APP. When the application starts, the first window in the main window is pushed into the rollback stack. When other windows of the application are displayed, the window is created in the display order and pushed into the rollback stack. The top-level window of the stack is rolled back to obtain the focus and can interact with users. When you execute the Activity finish method or press the Back key, the top-level window will be destroyed, and the next window will get the focus to interact with the user. For example, an APP has three windows: A, B, and C, where A is the main window. When B is started from A, C is started from B, and A is started from C, at this time, the rollback stack order for the A-B-C-A, the top and the bottom are A, when the Back, the top A will be the destruction of the stack, the window C will get the focus, in the Back, C will go out of the stack, and so on. When A finally goes out of the stack, the stack will be destroyed, the program will exit, and the corresponding resources will be released.

Normally, if the APP does not jump to a window, the new window will be pushed in after the stack rollback, however, you can set the window startup mode by setting the launchMode attribute of the window or setting the Intent setFlags () method during the jump to change the default rollback stack operation.

 

LaunchMode has four attributes: standard, singleTop, singleTask, and singleInstance. standard is the default start mode. SingleTop mode:

When the Startup Mode of an Activity is set to singleTop, when it is started, the system checks whether the window is at the top of the rollback stack. If yes, no new window is created and pushed to the rollback stack, instead, it directly displays the top-level window and calls its onNewIntent method. If not, a new window is pushed back to the stack. For example, an application has two activities (A, B), where the Startup Mode of B is singleTop. The user executes the following operations: the main window starts B, and B starts B, the order after these rollback stacks are executed is A-B, not A-B.


SingleTask mode:

The singleTask mode is more complex and can be divided into the following situations,

In the same application, jump to a window defined as singleTask mode, and check whether the window exists in the rollback stack. If yes, all the windows above will be destroyed, obtain the focus from the top layer of the rollback stack and call the onNewIntent method of the window. If an application has three activities (A, B, C), where B is started as singleTask, the user executes the following operations: Main Window A starts B, B starts C, C starts B, the window sequence in the rollback stack is A-B.

If a window declared as singleTask mode for another application is called, if the window exists, the stack (task) will be rolled back to the task in which the window is located. If the window does not exist, a rollback stack will be created and pushed to the new rollback stack. The current task will also be switched to the new rollback stack.

SingleInstance mode:

The singleInstance mode is very similar to the singleTask mode. The difference is that the rollback stack created in singleInstance mode can only contain one window. It seems that this mode is rarely used.

 

 

Use the setFlags () method of Intent to set the window startup mode. Commonly used:

Intent. FLAG_ACTIVITY_SINGLE_TOP

Intent. FLAG_ACTIVITY_CLEAR_TOP

Intent. FLAG_ACTIVITY_NEW_TASK

Intent. FLAG_ACTIVITY_CLEAR_TASK

Intent. FLAG_ACTIVITY_REORDER_TO_FRONT

Intent. FLAG_ACTIVITY_SINGLE_TOP: The same as setting singleTop in launchMode.

Intent. FLAG_ACTIVITY_CLEAR_TOP:

When you use Intent. the FLAG_ACTIVITY_CLEAR_TOP Activity checks whether the rollback Stack has a window. If yes, it clears the window and itself, and then pushes the window into the rollback stack. If window A jumps to B, B jumps to C, and C is switched to B (Intent is set during the jump. FLAG_ACTIVITY_CLEAR_TOP), where the rollback stack order is: A-B and the onCreate method of B is executed.

When Intent. FLAG_ACTIVITY_CLEAR_TOP and Intent. FLAG_ACTIVITY_SINGLE_TOP

At the same time, it is the same as setting singleTask in launchMode and redirecting to a window in the program.

 

 

Intent. FLAG_ACTIVITY_NEW_TASK:

The window function is the same as setting singleTask in launchMode and calling the singleTask declared by other applications.

 

Intent. FLAG_ACTIVITY_CLEAR_TASK:

All windows in the rollback Stack are cleared, and the windows to be displayed are created and pushed to the rollback stack. But must be

Intent. FLAG_ACTIVITY_NEW_TASK can be used together.

 

 

Intent. FLAG_ACTIVITY_REORDER_TO_FRONT

Interesting. When Intent. FLAG_ACTIVITY_REORDER_TO_FRONT is set for redirection, the system checks whether the window exists in the rollback stack. If not, the new window is pushed into the rollback stack. If yes, the window will be mentioned to the top layer of the rollback stack. For example, window A to B, B to C, C to B (set Intent. FLAG_ACTIVITY_REORDER_TO_FRONT), then the rollback stack ORDER FOR THE A-C-B, rather than the A-B-C-B.

 

 

 

 

 

 

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.