Apps for Android official Docs (Tasks and back Stack)

Source: Internet
Author: User
Tags home screen

An application contains multiple instances of activity, each of which has its own action, and each activity can initiate other activity, such as an email application that contains an activity that displays a list of email messages. When the user clicks on an item in the list, the activity that displays the details is started.

This article will cover the activity stack and the back stack (tasks and backs stack), you need to access the official text, you can click on this link: "Tasks and" the stack.

An activity can even initiate activity in other applications. For example, if you want to start an activity that sends a message, use intent implicitly to start it, which should contain an action with a value of "send" and contain some data such as the email address and the content of the message. In this way, the activity of the initiated email is as if it were part of the application. Android uses the same task stack to enable seamless switching between activity (Android maintains this seamless user experience by keeping both activities in the same Task).

Task stack and return stack (tasks and back stack)

The task stack is a place where the activity collection is stored. With a task stack, the user can complete an operation (such as the operation of sending a text message, the operation of a photograph, etc.) (a task is a collection of activities that the users interact with when performing A Certa In job).

The home button is where most of the task stacks start (The Device Home screen is the starting places for most tasks). When the user clicks on an app's icon, the application's task stack is switched to the foreground, and if the task stack does not exist (i.e. the user has never started the app), then a new task stack will be created, and the application's main activity will be pressed to the bottom of the task stack (a new task is created And the "main" activity for this application opens as the root activity in the stack).

When activity A starts activity B, activity B is pushed to the top of the stack and gets the focus. Activity A is under activity and is in the stop state, and activity in the stop state still holds information that interacts with the user. When the user taps the back key, Activity B pops out of the stack and resumes the resume state by destroy,activity a. The order of activity in the stack is never rearranged (activities in the stack is never rearranged), activity can only be popped out of the stack or pressed (only pushed and popped from the S Tack). The task stack follows the principle of LIFO (last in, first out). As shown in the following:

When the user taps the back key, the activity in the stack is popped up in turn until it is returned to the desktop. After all activity has been ejected, the task stack will no longer exist (when all activities is removed from the stack, the task no longer exists).

A task stack is a tightly knit set of cells: When a user initiates an activity or clicks the Home button to return to the desktop, the task stack to which the activity belongs is moved to the foreground or back to the background as a whole. All activity in the task stack moved to the background will be in the stop state. But the contents of the stack remain intact, they just lose focus, as shown, activity in the background in task A is only in the stop state, and they still retain all of the activity's information:

Android can process multiple task stacks at once in the background. However, if too many task stacks are managed in the background, the system may destroy some activity to free up memory, causing the state of the activity to be lost.

Because the order of activity cannot be changed in the task stack, by default, if an activity of another application needs to be started multiple times in one application, the activity will be re-instantiated every time it is started, as shown in:

Of course, you can also change this default rule so that activity instances in a task stack cannot be duplicated. This is described below.

Here's a summary of the default rules for activity and task stacks:

    • When activity a starts activity B, activity A is in the stop state, but the information in activity A is retained, such as the information the user types or the location of the slide. If the user clicks the back key in activity B, activity a resumes status.

    • When the user clicks on the home key, the activity and the task stack to which it belongs will be integrated into the background and in the stop state. The state of all activity in the stack is preserved. If the user resumes the application by tapping the app icon, the task stack is switched to the foreground and the activity is on top of the stack.

    • Activity can be instantiated several times.

Save activity status (saving activity State)

As mentioned above, activity that is not at the top of the stack will be saved by the system and the contents of the activity will be restored when the activity is back on top of the stack. In fact, the developer should actively save state information in the activity through the callback method in the activity.

When activity is in the stop state, the system takes precedence over its instance to free up memory. In this case, the status information of the activity will be lost. However, the task stack still records the location of the activity on the stack, and when it reverts to the foreground, the instance of the activity is re-created (not a resume) and its contents are lost. To avoid content loss, you should override the activity's callback method onSaveInstanceState() .

Management task stack (managing tasks)

The default behavior of the task stack is described above, which can already handle most cases, but sometimes you want to break this default behavior, such as creating a new task stack when you start an activity in your app, or starting an existing activity, and you want to get it to the foreground directly. Instead of creating a new activity instance, or exiting the task stack, you want the rest of the activity to be out of the stack, in addition to the main activity at the bottom of the stack.

To do this, you need to configure it in the label of the manifest file <activity> , or starActivity() add flag to the intent parameter passed in the method.

<activity>The main attributes of the task stack in the tag are as follows:

    • taskAffinity

    • launchMode

    • allowTaskReparenting

    • clearTaskOnLaunch

    • alwaysRetainTaskState

    • finishOnTaskLaunch

The flags of the commonly used intent are as follows:

    • FLAG_ACTIVITY_NEW_TASK

    • FLAG_ACTIVITY_CLEAR_TOP

    • FLAG_ACTIVITY_SINGLE_TOP

! Please note: Many applications prohibit changing the default behavior of their activity and task stacks. If you really need to change its behavior, you need to test the back key, home key, and so on to prevent conflicts with the default behavior.

Defining the startup mode (defining launch Modes)

The startup mode specifies the associated mode of the newly started activity and the current task stack. There are two ways to define the startup mode:

    • Configured in manifest (Using the manifest file);

    • Use the flag parameter of the Intent (using Intent flags);

For example, activity A starts activity B, and you can specify how activity B is managed with the current task stack in manifest of activity B, or you can specify activity through the intent of activity a How B is managed with the current task stack. If the program is configured in both ways, the specified mode of intent (activity A's request (as defined in the intent) is honored over Activity B's request (as Def Ined in its manifest).

! Note: Some boot modes can only be configured in manifest, not in flag intent, and some boot modes can only be specified in intent flag and cannot be configured in manifest.

Configuring the boot mode using the manifest file (using the manifest files)

Configured in the properties of the <activity> tag launchMode , which specifies how activity is started in the task stack in four ways:

    • standard(default): Activity can be instantiated multiple times, each instance can exist on different task stacks, and each task stack can contain multiple instances of the same activity.

    • singleTop: If the activity instance is already at the top of the stack, the activity simply callbacks its onNewIntent() method and no longer creates a new instance. The activity can be instantiated several times, each of which can exist on different task stacks, and each task stack can contain multiple instances of the same activity, except for instances at the top of the stack.

    • singleTask: The system creates a new task stack and puts the activity at the bottom of the stack. If the activity instance already exists in the other task stack, the activity instance will not be created again, but the callback onNewIntent() method. An activity instance can have only one at a time.

    • singleInstance: singleTask similar to pattern, the only difference is that if you press the activated activity into the newly created stack (that is, the activated activity instance does not exist), then there is only one activity instance in the stack and no more instances.

For example, each activity in an Android browser application is designated as a singleTask startup mode. This means that if your activity is intended to activate activity in an Android browser application, the activity that starts will not appear in your application, but is either pressed into the newly created task stack. Either the task stack that will start the activity is cut to the foreground (the latter indicates that the activity to be started has been started, only in the background).
In either case, when you press the Back button, the system always cuts to the activity in the foreground task stack. If you specify the startup mode for an activity and singleTask the instance already exists, the system will switch the task stack that the activity belongs to to the foreground as a whole. As shown in the following:

Configure the startup mode using the flags parameter contained in Intent (using Intent flags)

In addition to using the startup mode for configuring activity in manifest, you can also use the flags parameter contained in intent in your code and eventually pass that intent into the startActivity() method to configure the startup mode. The commonly used flag parameters are as follows:

    • FLAG_ACTIVITY_NEW_TASK: Creates a new task stack and presses the activity into the bottom of the stack. If the activated activity already exists in memory, the system cuts the task stack on which the activity is located to the foreground and recalls the method of the activity instance onNewIntent() . This is equivalent to configuring the singleTask boot mode in manifest.

    • FLAG_ACTIVITY_SINGLE_TOP: If the activated activity is at the top of the stack, no activity instance is created, but only its method is recalled onNewIntent() . This is equivalent to configuring the singleTop boot mode in manifest.

    • FLAG_ACTIVITY_CLEAR_TOP: If the activity to be started already exists in the current task stack, then all activity instances above the activity are destroy, then the activity instance will be at the top of the stack and the intent will be returned to the activity's onNewIntent() Method. This does not match the boot mode in the manifest. FLAG_ACTIVITY_CLEAR_TOP often used in conjunction with the FLAG_ACTIVITY_NEW_TASK Union.

Handling affinities (Handling affinities)

Affinities indicates which task stack an activity "expects" to belong to. By default, all activity for the same app has the same affinity. That is, by default, all activity for the same app is expected to belong to the same task stack. However, you can modify the default settings. For example, activity from different applications can have the same affinity, or different activity for the same application has different affinity.

You can configure properties in the Activity tab taskAffinity to change the activity's affinity. taskAffinityproperty is a string, you must ensure that the <manifest> package name specified in the label is unique, because Android affinity the package name as the task stack.

The affinity will work in the following two scenarios:

    • When the boot intent contains FLAG_ACTIVITY_NEW_TASK the flag. This indicates that the activated activity always "wants" to push itself into a new stack. However, if the Affinity property of a task stack exists in the system with the same activity, the activity will be pushed into the task stack. Without such a task stack, the system creates a new task stack and pushes the activity into the bottom of the stack.
      If the flag causes the activated activity to press into the newly created task stack, and the user clicks on the home key, it is necessary to switch the activity to the foreground in some way. Some entities, such as Notification Manager, typically launch activity into the bottom of a new stack. Since these start-up notification are not part of this application, flag is usually added in intent FLAG_ACTIVITY_NEW_TASK . If your activity can be initiated by such an external entity, be careful that the user can also launch it in a unique way (such as adding a property to the intent filter CATEGORY_LAUNCHER , click the Start icon, All activity stacks that are located at the bottom of the stack are eligible for an implicit launch condition.
    • When the activity's allowTaskReparenting property is true . At this point, when the activity is started, the activity is the default task stack, and when the activity belongs to the application to the foreground, the activity is returned to the application task stack.
      For example, when you set the properties of an activity in the weather forecast app to allowTaskReparenting true start the activity, the activity originally belonged to your app's task stack, but when the task stack for the weather forecast application was cut to the foreground, This activity will be pressed into the task stack of the weather forecast application.
Clear the Back stack (clearing)

If the user does not cut the task stack to the foreground for a long time, the activity in the task stack will be destroy (does not include the start activity at the bottom of the stack). When the user cuts the task stack to the foreground, only the activity at the bottom of the stack is restored. Some properties are listed below to modify this default behavior:

    • alwaysRetainTaskState: If the property of the activity at the bottom of the stack is alwaysRetainTaskState set to True, the status information of all activity in the task stack will be preserved even if the task stack to which the activity belongs is placed in the background for a long time.

    • clearTaskOnLaunch: If the property of the activity at the bottom of the stack is set to True, the task stack will be the initial state when the task stack is cut to the clearTaskOnLaunch foreground, even if the user has cut it to the background for a short time, all the activity's status information in the task stack is cleared, and the task stack is then truncated to the front. in other words, this property is exactly the opposite of the alwaysRetainTaskState property.

    • finishOnTaskLaunch: This property clearTaskOnLaunch is similar, except that it is for a single activity.

Start the task stack (starting a task)

You can configure the start activity of the task stack as follows Intent-filter:

<activity  ...  > < Intent-filter  ...  > <action android:name= Androi D.intent.action.main "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter>  </activity>  

The user must be able to exit the task stack at any time and cut the task stack to the foreground with the launch icon at any time
(Users must is able to leave a task and then come back to it later using this activity launcher). Therefore, the "singleTask"、 "singleInstance" activity that uses these two startup modes must have ACTION_MAIN 和 CATEGORY_LAUNCHER the Intent-filter. For example, the user initiates a singleTask modal activity that is pressed into the bottom of a new task stack, and when the user has done some editing work in the activity, click the Home button to cut the task stack to the background. Then when the user wants to start the activity again, if the activity is configured ACTION_MAIN 和 CATEGORY_LAUNCHER , the user will not be able to start the activity! If you do want the activity to be cut into the background, the user can no longer cut it to the foreground, and in the Activity tab, finishOnTaskLaunch set the property to true .

Apps for Android official Docs (Tasks and back Stack)

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.