Android activity's task stack and four startup modes

Source: Internet
Author: User

In the Android system by default each time you start an activity, the system will create an instance, and follow the advanced principle into the task stack, when we press the back key, there will be an activity from the top of the task stack removed, repeat, until the task stack is empty, the system will recycle the task stack. However, it is not reasonable to create multiple instances repeatedly when the system starts the same activity multiple times, and Android provides four startup modes to modify the default behavior of the system in order to optimize the problem.

The four startup modes of activity are as follows: Standard, Singletop, Singletask, singleinstance

Android Task Stack

The task stack is a collection of all the activity in an Android app, and the Android system uses stacks to manage the activity, which is called the back stack, and the order of activity in the stack is stored in the order in which they were opened.

Features of the task stack:
    • The task stack is a record of the activity that the user has opened.
    • When an application is opened the system assigns him a task stack, and when all activity exits, the task stack is emptied.
    • When an activity initiates another activity, the new activity is placed at the top of the stack and will receive the focus. The previous activity remains in the return stack but is in a stopped state.
    • When the user presses the back key, the topmost activity in the stack is removed and the previous activity is returned to the topmost position.
    • The task stack is a typical last-in, first-out data structure. You can only add activity to the top of the stack or remove the activity at the top of the stack. Therefore, the return stack
    • The ID of the task stack is an integer data type that is self-growing.
    • In the Android operating system there will be multiple task stacks, one application a task stack.
    • The desktop app is the same as the general application, and the task stack behaves the same.
    • By default, an application is closed and the task stack for the application is emptied. The process of the application is also preserved.
    • When the program opens, a task stack is created to store the activity of the current program, and all activity belonging to the current program (including those invoked by the current application) belongs to a task stack.
    • A task stack contains a collection of activity that can be ordered to choose which activity interacts with the user, and only the activity at the top of the task stack can interact with the user.
    • The task stack can be moved to the background and retains the status of each activity. And the user is ordered to list their tasks, and they do not lose their status information.
    • When exiting the application, the task stack is destroyed and the program exits when all activity in the task stack is cleared out of the stack.
standard-Standard Mode

This mode is the default startup mode, that is, the standard mode, without specifying the startup mode, the system uses this mode to start the activity by default, each time the activity is started will be rewritten to create a new instance, regardless of the existence of this instance, this mode, Who initiates the activity of the pattern, which is in the task stack of the activity that initiated it. In the standard mode task stack, the activity is created regardless of whether or not the activity is added to the stack, which is called by the OnCreate (), OnStart (), Onresume () method.

# # singletop-Stack top multiplexing mode

In this mode, if the activity to be started already exists on the top of the stack, then the activity will not be recreated, but the instance of the top of the stack is reused, and the intent object will be passed in, callback the Onnewintent method, It is important to note that this activity OnCreate (), the OnStart () method will not be called because it does not change. If an instance of the activity does not exist at the top of the stack, the case is the same as standard mode, creating a new activity instance.

Keywords: top of stack

singletask-Stack Internal multiplexing mode

In this mode, if the activity to be started has an instance in the task stack, then the activity is not recreated, but instead removes all the activity on that instance from the (destory) task Stack, Place the target instance at the top of the stack and call its onnewintent () method directly. New if not present.

There is only one instance in the same task stack.

singleinstance-Single-Case mode

This pattern has all the features of the Singletask mode, and the difference is that the activity in this mode occupies a single task stack, with global uniqueness, that is, an instance of the whole system, due to the characteristics of the reuse of the stack, Subsequent requests do not create a new activity instance unless the particular task stack is destroyed. Activity initiated in SingleInstance mode is a singleton in the entire system, and if an instance is already in place when the Activiyt is started, it will be dispatched to the foreground and reused for this instance.

Android activity's task stack and four startup modes

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.