当前位置:操作系统 > 安卓/Android >>

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.
这个属性和clearT

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,