android task與back stack 開發文檔翻譯 – 3

andling affinities

The affinity indicates which task an activity prefers to belong to.
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.

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

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 affinity comes into play in two circumstances:

1.When the intent that launches an activity 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.
它被推入與調用者相同的back stack中。

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).
一些實體(例如notification manager)總是在外部task打開activity,從來不作為他們自己的一部分,所以他們總是在intent中加入FLAG_ACTIVITY_NEW_TASK傳遞給startActivity()
******如果你有一個可以被可能含有此標識的外部實體調用的activity,註意用戶有一種不受約束的方式來返回到開始的task ,例如通過launcher圖標(task的根activity有一個CATEGORY_LAUNCHER的intent過濾器;參見下面的開啟一個 task章節)

2.When an activity has its allowTaskReparenting attribute 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.

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 an .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".

Clearing the back stack
清除back stack

If the user leaves a task for a long time, the system clears the task of all activities except 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.

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

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 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.

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.

Starting 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:

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 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", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter.
Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends 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.

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).
由於這些原因  你不希望用戶返回到activity的地方,設置<activity>元素的finishOnTaskLaunch屬性為true(見清除back stack)。

作者: su1216