Android task and back stack development documentation translation-3

Source: Internet
Author: User

Andling affinities
Processing affinity

The affinity indicates which task an activity prefers to belong.
By default, all the activities from the same application have an affinity for each other.
So, by default, all activities in the same application prefer to be in the same task.
However, you can modify the default affinity for an activity.
Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different task affinities.
Affinity indicates which task an activity tends to belong.
By default, all the activities in the same application have the same affinity.
By default, all the activities in the same application tend to be in the same task.
However, you can modify the default affinity of an activity.
Activity defined in different applications can share the same affinity, or activity defined in the same application can be specified with different tasks affinity

You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.
You can use the taskAffinity attribute of the <activity> element to modify the affinity of any known activity.


The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that name to identify the default task affinity for the application.
The taskAffinity attribute is a String type different from the default package name in the <manifest> element, because the system uses that name as the application to identify the default task affinity


The affinity comes into play in two circumstances:
Affinity

1. When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.
When the intent of the activity is started, it contains the FLAG_ACTIVITY_NEW_TASK flag.

A new activity is, by default, launched into the task of the activity that called startActivity ().
It's pushed onto the same back stack as the caller.
However, if the intent passed to startActivity () contains the FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to house the new activity.
Often, it's a new task.
However, it doesn' t have to be.
If there's already an existing task with the same affinity as the new activity, the activity is launched into that task.
If not, it begins a new task.
By default, a new activity is started into the task that calls startActivity.
It is pushed into the same back stack as the caller.
However, if the intent passed to startActivity () contains the FLAG_ACTIVITY_NEW_TASK flag, the system will find a different task to store the new activity.
It is often a new task.
But not necessarily.
If a task with the same affinity as the new activity already exists, the activity will be started into the task.
If this is not the case, a new task will be started.


If this flag causes an activity to begin a new task and the user presses the Home button to leave it, there must be some way for the user to navigate back to the task.
Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always put FLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity ().
If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started, such as with a launcher icon (the root activity of the task has a CATEGORY_LAUNCHER intent filter; see the Starting a task section below ).
If this flag causes an activity to start a new task and the user presses the home Key to exit it, you must provide some ways to navigate back to the task.
Some entities (such as notification manager) always open the activity in external tasks and never act as a part of themselves. Therefore, they always add FLAG_ACTIVITY_NEW_TASK to the intent and pass it to startActivity ()
* ***** If you have an activity that can be called by an external entity that may contain this identifier, note that you have an unrestricted way to return to the start task, for example, use the launcher icon (the root activity of a task has an intent filter of CATEGORY_LAUNCHER; see the following section to enable a task)


2. When an activity has its allowTaskReparenting attribute set to "true ".
When the allowTaskReparenting attribute of an activity is set to true


In this case, the activity can move from the task it starts to the task it has an affinity for, when that task comes to the foreground.
In this case, when the task comes to the foreground, the activity can move from the task it starts to a task with its affinity.

For example, suppose that an activity that reports weather conditions in selected cities is defined as part of a travel application.
It has the same affinity as other activities in the same application (the default application affinity) and it allows re-parenting with this attribute.
When one of your activities starts the weather reporter activity, it initially belongs to the same task as your activity.
However, when the travel application's task comes to the foreground, the weather reporter activity is reassigned to that task and displayed within it.
Tip: If. apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application ".
For example, assume that an activity that reports weather conditions after a city is selected is defined as part of a tourism application.
It has the same affinity as other activities in the same application (the default affinity of the Application), and allowTaskReparenting = true.
When a weather forecast activity is enabled for one of your activities, it initially belongs to the same task as your activity.
However, when the travel application task comes to the foreground, the weather forecast activity is reassigned to this task and displayed inside it.
Tip: if an apk file contains multiple "Applications" for users, you may want to use the taskAffinity attribute to allocate different affinity to each "application" associated with the activity.


Clearing the back stack
Clear back stack

If the user leaves a task for a long time, the system clears the task of all activities cannot the root activity.
When the user returns to the task again, only the root activity is restored.
The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.
If the user leaves the task for a long time, the system clears all the other activities except the root activity in the task.
When the user returns to the task again, only the root activity is restored.
The reason why the system has this behavior is that after a long period of time, users are likely to give up what they have done and return to the task to start doing new things.

There are some activity attributes that you can use to modify this behavior:
Some activity attributes can modify this behavior:

AlwaysRetainTaskState
If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen.
The task retains all activities in its stack even after a long period.
If the attribute of the Root activity of the task is set to true, the default behavior described previously will not occur.
Even after a long period of time, the task will keep all the activities in its stack


ClearTaskOnLaunch
If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it.
In other words, it's the opposite of alwaysRetainTaskState.
The user always returns to the task in its initial state, even after a leaving the task for only a moment.
If the attribute of the Root activity of a task is set to true, the stack is cleared and only the root activity is left when the user leaves the task and returns to it.
In other words, it is opposite to alwaysRetainTaskState.
The user always returns the initial status of the task, even if the task is left for a short time.


FinishOnTaskLaunch
This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task.
It can also cause any activity to go away, including the root activity.
When it's set to "true", the activity remains part of the task only for the current session.
If the user leaves and then returns to the task, it is no longer present.
This attribute is similar to clearTaskOnLaunch, But it operates on a single activity rather than the entire task.
It also causes the activity to exit, including the root activity.
When finishOnTaskLaunch is set to true, the activity only maintains some tasks for the current session.
If the user leaves and returns to this task, the activity will no longer exist.


Starting a task
Start a task

You can set up an activity as the entry point for a task by giving it an intent filter with "android. intent. action. MAIN "as the specified action and" android. intent. category. LAUNCHER "as the specified category. for example:
You can set an activity as the task entry by specifying an action as android. intent. action. MAIN and category as the intent filter of android. intent. category. LAUNCHER.


An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.
This kind of intent filter causes an icon and tag of the activity to be displayed in the launcher application, giving the user a way to start the activity and return the task created after the activity is started at any time.


This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher.
For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and "" singleInstance ", shocould be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter.
Imagine, for example, what cocould happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user has Ds some time working in that task.
The user then presses the Home button.
The task is now sent to the background and is not visible.
Now the user has no way to return to the task, because it is not represented in the application launcher.
The second point is very important: the user must be able to use this activity starter to exit the task and then return to the task.
For this reason, singleTask and singleInstance start modes mark activity as always starting a task and should only be applied when activity has ACTION_MAIN and CATEGORY_LAUNCHER filters.
Think about what will happen if there is no filter: an intent starts an activity in singleTask mode and initializes a new task. The user spends some time on this task.
Then press the home Key.
Now this task is sent to the background and is no longer visible.
The user cannot return to this task because it is not displayed in the launcher application.

For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" (see Clearing the stack ).
For these reasons, you do not want the user to return to the activity. Set the finishOnTaskLaunch attribute of the <activity> element to true (see clearing back stack ). Www.2cto.com


Author: su1216

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.