) Tasks and activity stacks in Android

Source: Internet
Author: User
As mentioned above, one activity can start another, including those defined in different applications. Suppose, for example, you want the user to display the street map of some places. There is already an activity that can do this, so all you need to do is put behavior objects and required information together and pass them to startactivity (). The map viewer displays the map. When you press the back button, your activity is displayed on the screen again.
For users, this map viewer looks like a part of your application, even if it is defined in another application and runs in the Process of that program. Android maintains the user experience by keeping all activities in the same task. Simply put, a task is the "application" that the user experiences ". It is a group of related activities and is allocated to a stack. The root activity in the stack is the start of the task-generally, it is the activity selected in the user group Application Loader. The activity at the top of the stack is the currently running one that processes user actions in a centralized manner. When an activity starts another activity, the new activity is pushed to the stack and becomes a running activity. The previous activity is retained in the stack. When you press the back button, the current activity will pop up in the middle stack, and the previous activity will return to the running status.
The stack contains objects. If an instance with more than one subclass of the same activity on a stack opens -- for example, multiple map viewers -- each stack has its own portal for each instance. The activities in the stack cannot be rearranged, but can only be pushed in or popped up.
A task is a stack composed of some activities, not a class or element in the list file. Therefore, there is no way to set the value of an activity independent of the activity it contains. The task value is set as a whole in the root activity. For example, the next section will discuss the "affinity of tasks". This value is read from the root activity affinity.
All activities in a task are used as a unit. The entire task (the entire activity stack) can be moved to the foreground or background. Suppose, for example, the current task has four activities in the stack-three under the current activity. The user presses the Home Key, enters the Application Loader, and selects a new program (in fact, it is a new task ). The current task enters the background, and the root activity of the new task is displayed. Then, after a while, the user returned to the main interface, and re-selected the previous application (the previous task), the task with four activities in the stack, and now appeared at the front end. When you press the back button, the screen will not show the activity you just left, but delete the activity at the top of the stack. The previous activity in the same task will be displayed.
The actions described above are the default actions of activity and task. But there are also ways to modify all its aspects. The association between activity and task. Activity behavior in the task is controlled by the identifier of the activity object to be started and the interaction of the attribute of the <activity> element in the list file. The requester and the corresponding user must describe what happened.

Here, the main behavior signs are:

Flag_activity_new_task
Flag_activity_clear_top
Flag_activity_reset_task_if_needed
Flag_activity_single_top

The main <activity> attributes are:

Taskaffinity
Launchmode
Allowtaskreparenting
Cleartaskonlaunch
Alwaysretaintaskstate
Finishontasklaunch

The following section describes the functions of these labels and attributes, how they affect each other, and how they can be used to control their use.

Affinity and new tasks

By default, all the activities in the application have an affinity for other activities-this is a priority for other activities in the same task. Then, you can use the taskaffinity attribute of the <activity> element to set the affinity for each activity. Activity defined by different applications can share the same affinity, or activity defined by the same application can specify different affinity. Affinity works in two situations: when the behavior object starts an activity that contains the flag_activity_new_task flag, and when the allowtaskreparenting attribute of an activity is set to "true ".

Flag_activity_new_task flag

As described above, a new activity is loaded into the task where the activity object that calls the startactivity () method is located by default. It is pushed into the same stack as the caller. However, if the behavior object passes the flag_activity_new_task tag when calling the startactivity () method, the system will use a different task to accommodate this new activity. Generally, it is represented by the name of the tag. It is a new task, but it does not have. If a task has the same affinity as the activity, the activity will be loaded into the task. If not, the new task will be started.

Allowtaskreparenting attribute

If the allowtaskreparenting attribute of the activity is set to "true", it can move from the task in which the activity was started to another task that appeared in the foreground. For example, assume that an activity can be used as part of a travel application based on the selected city including weather conditions. It has the same affinity (default affinity) as other activities in the same application and allows reorganization. One of your activities has the weather reporter enabled, so it belongs to this activity in the same task. However, when the travel application starts to run, the weather report is reassigned and displayed in that task.

Startup Mode

Four different startup modes can be assigned to the launchmode attribute of the <activity> element.

"Standard" (default mode)
"Singletop"
"Singletask"
"Singleinstance"

The main differences between these modes are as follows:

Which task stores the activity to respond to the behavior. For the "standard" and "singletop" modes, this task is the one that generates behaviors (and calls startactivity () unless the behavior object contains the flag_activity_new_task tag. In this case, as described in affinities and new tasks in the previous section, a different task will be selected.
Can they have multiple instances. Activity of the "standard" and "singletop" types can be instantiated multiple times. They can belong to multiple tasks. A specific task can also have multiple instances of the same activity.
Compared with "singletask" and "singleinstance", only one instance is allowed for an activity. Because these activities are the root of the task. This limit means that the device cannot have more than one task instance at the same time.
Whether other activities can be included in the task. A "singleinstance" type activity is the only activity in its task. If it starts other activities, regardless of the Startup Mode of the activity, it will be loaded into a different task-as if the flag_activity_new_task tag in the behavior object. In other aspects, the "singleinstance" and "singletask" modes are the same.
The other three running tasks have multiple activities. "Singletask" is always the root activity in the task, but it can start other activities and assign them to the task in which it is located. Activity of the "standard" and "singletop" types can appear anywhere in the task.
Whether to start a new instance to process a new behavior. For the default "standard" mode, a new instance is created for each action to respond. Each instance processes only one action. For the "singletop" mode, if an existing instance is located at the top of the stack of the target task activity stack, it will be reused to process this behavior. If it is not at the top of the stack, it will not be reused. Instead, it creates a new instance for the behavior and pushes it into the stack.

For example, assume that the activity stack of a task is composed of the Root Activity A and B, C, and D in the order from top to bottom. Therefore, the stack is a-B-c-d. An activity that points to type D. If D is the default "standard" loading mode, a new instance will be started, and the stack is now a-B-c-d. However, if D's loading mode is "singletop", an existing instance will be used to handle this behavior (because it is at the top of the stack) and the stack should also be a-B-c-d.
As mentioned above, "singletask" and "singleinstance" types of activity have only one instance at most, so their instances should process each new behavior. "Singleinstance" type activity is always at the top of the stack (because it is the only activity in the task), so it is always able to properly handle the behavior. However, an activity of the "singletask" type may have other activities on it. If this is the case, the action cannot be handled and is discarded. (Even if this behavior is discarded, the arrival of it will lead to those tasks that should be retained and displayed to the foreground ).

When an activity is required to process a new behavior, the behavior object will be passed in by calling the onnewintent () method of the activity (the behavior that initially started the activity can be passed by calling getintent () method ).

Note: When you create a new activity instance to process a new activity, you can always press the back button to return to the previous status (previous activity ). However, when an existing activity instance processes a new behavior, you cannot press the back button to return to the previous status.

For more information about the loading mode, see <activity>.

Clear Stack

If the user leaves a task for a long time. The system clears all activities except the root activity. When the user returns to the task, it is like the user has left the task, except for the initial activity. This idea is like this. After a while, users may give up what they did and return to the task to do new things.

This is only the default situation. Some activity attributes can be controlled and modified.

Alwaysretaintaskstate attribute

If this attribute of the Root Activity of a task is set to "true", the default behaviors mentioned above will not happen. This task retains all the activities, even after a long period of time.

Cleartaskonlaunch attributes

If this attribute of the Root Activity of the task is set to "true", all the activities except the root activity are cleared as long as the user leaves the task and returns the result. In other words, it is the opposite of alwaysretaintaskstate. When the user returns to the task, it always returns to the Initial State, regardless of the time it leaves.

Finishontasklaunch attributes

This attribute is similar to cleartaskonlaunch, But it acts on a single activity rather than the entire task. It can cause any activity to exit, including the root activity. When it is set to "true", the activity as part of the task is only valid for the current session. If the user leaves and then returns to the task. It will no longer appear.

There are other ways to force the activity to be removed from the stack. If a behavior object contains the flag_activity_clear_top flag, its target task already has an activity instance of this type, and all the activities on this instance in the stack will be cleared, therefore, this instance will appear at the top of the stack and respond to the behavior. If the activity is designed to be in the "standard" mode, it will also be cleared from the stack and a new instance will be started to process the incoming behavior. This is because when the "standard" mode is set, a new instance is created for each new behavior.

Flag_activity_clear_top is often used with flag_activity_new_task. When used together, these marks are a way to locate an activity that exists in another task and place it in a place that can respond to behavior.

Start a task

Activity sets the action filter "android. Intent. Action. Main" to the specified action and "android. Intent. Category. launcher" to become the entry of the task. (Examples discussed above about the behavior filter ). This type of filter will display the icons and labels of the activity on the Application Loader, allowing the user to start and return the activity.

The second capability is more important. Users should be able to return after leaving a task for a while. In this way, the "singletask" and "singleinstance" modes that can initialize the task can only be used in the activity with the main and launcher filters. Imagine what would happen without these two filters: An activity that starts the "singletask" mode, starts a new task, and the user spends some time on the task. Then the user presses the Home key, and the task is hidden in the background. Because it is not displayed on the Application Loader, there is no way to return to this task.

A similar annoyance flag_activity_new_task flag. If this flag causes the activity to start a new task and the user presses the Home Key to exit it, there must be some methods to direct the user back. Some entities (such as the notification manager) always start the activity in an external task instead of being part of them, so they always pass behavior objects marked with flag_activity_new_task to startactivity (). If you have an activity that can be called by an external entity using this label, you should be aware that you should have a way to return the started task.

If you do not want the user to return to the activity, set the finishontasklaunch attribute of <activity> to "true". For more information, see the previous section "Clear stacks.

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.