android task与back stack 开发文档翻译 - 3
由于本人英文能力实在有限,不足之初敬请谅解
本博客只要没有注明“转”,那么均为原创,转贴请注明链接
android task与back stack 开发文档翻译 - 1
android task与back stack 开发文档翻译 - 2
android task与back stack 开发文档翻译 - 3
andling affinities
处理affinity
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.
affinity表明了一个activity倾向属于哪一个task
默认的,在同一个应用中所有的activity有相同的affinity
所以默认的,同一个应用中所有的activity倾向于在相同的task中。
然而,你可以修改activity的默认affinity
在不同应用中定义的activity可以共用同一个affinity,或者定义在同一个应用中的activity可以被指定不同的任务affinity
You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.
你可以通过<activity>元素的taskAffinity属性修改任何已知的activity的affinity
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.
taskAffinity属性为一个与在<manifest>元素中默认包名不同的String类型,因为系统使用那个名字为应用标识默认任务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.
当启动activity的intent包含FLAG_ACTIVITY_NEW_TASK标志。
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.
默认的,一个新activity会启动到调用startActivity()的activity的task中。
它被推入与调用者相同的back stack中。
然而,如果传递给startActivity()的intent包含FLAG_ACTIVITY_NEW_TASK标志,系统会寻找一个不同task来储存新activity。
经常是一个新的task
但是并不一定是这样。
如果已经存在一个与新activity相同affinity的task,那么这个activity会被启动到此task中。
如果不是这样,那就会启动一个新的task。
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).
如果这个标志导致一个activity开启一个新的task并且用户按home键离开它,这里必须给用户提供一些方式可以导航回这个task。
一些实体(例如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".
当一个activity的allowTaskReparenting属性设置为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.
这种情况下,当那个task来到前台时,activity可以从它开始的task中移动到一个有为它的affinity的task中。
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".
例如:假设一个选择城市后报告天气情况的activity被定义为一个旅游应用的一部分。
它与同应用中的其他activity拥有相同的affinity(应用默认的affinity),并且allowTaskReparenting=true。
当你的一个activity开启天气预报activity时,最初它与你的activity属于同一个task。
但是,当这个旅游应用的task来到前台时,这个天气预报activity被重新分配到此task中并且在其内部显示。
提示:如果一个apk文件对于用户来说包含多个“应用”,你也许想要使用taskAffinity属性来分配不同的affinity到与activity关联的每个“应用”中。
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.
如果用户离开task很长时间,系统清理除了task中的根activity的其他所有activity
当用户再次返回到task中,只有根activity被恢复。
系统之所以有这种行为是因为经过较长时间后,用户很可能要放弃他们之前所做的,返回到task开始做些新的事情。
There are some activity attributes that you can use to modify this behavior:
一些activity属性可以修改这种行为:
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.
如果task的根activity的这个属性被设置为true,之前描述的默认行为就不会发生
即使在很长一段时间后,task也会保持在其stack内的所有activity
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.
如果task的根activity的这个属性被设置为true,每当用户离开task再返回到它,stack都会被清除只留下根activity。
换句话说,它与alwaysRetainTaskState相反。
用户总是返回到task的初始状态,即使是只离开task一小会。
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.
这个属性和clearTaskOnLaunch相似,但是它操作单一的activity而不是整个task。
它也会导致activity离开,包括根activity。
当finishOnTaskLaunch设置为true时,activity只为当前会话保持部分task。
如果用户离开又返回到这个task,这个activity将不复存在。
Starting a task
开始一个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:
你可以通过指定一个action为android.intent.action.MAIN,category为android.intent.category.LAUNCHER的intent过滤器,设置一个activity作为task入口。
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.
一个这种intent过滤器导致activity的一个图标和标签显示在launcher应用中,给用户一种方式来启动activity和任何时候返回到activity被启动之后它建立的task。
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.
第二点很重要:用户必须有能力使用这个activity起动器离开task之后再返回到这个task。
由于这个原因,singleTask和singleInstance两种启动模式把activity标记成总是开始一个task,应该只被应用于当activity有ACTION_MAIN和CATEGORY_LAUNCHER过滤器时。
想一下,例如,如果没有过滤器将会发生什么:一个intent启动一个启动模式为singleTask的activity,初始化一个新的task,用户花费一些时间在这个task上面
然后用户按home键。
现在这个task被送到后台不再可见。
现在用户没有办法返回到这个task上了,因为它没有在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)。
本文来自对下面地址的翻译,英文水平实在有限,希望拍砖同时能给予指正。
http://developer.android.com/guide/components/tasks-and-back-stack.html
转贴请保留以下链接
本人blog地址
下一篇: Oracle-物化视图