Application Fundamentals--Affinities and new tasks

Affinities and new tasks

By default, all the activities in an application have an affinity for each other — that is, there's a preference for them all to belong to the same task. However, an individual affinity can be set for each activity with the taskAffinity attribute of the <activity> element. Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different affinities. The affinity comes into play in two circumstances: When the Intent object that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag, and when an activity has its allowTaskReparenting attribute set to "true".

翻译:默认一个应用中的所有Activity组件相互之间有同一个affinity,换句话说manifest文件中所有Activity组件同属一个task,但是你还是可以手动给Activity组件设定affinity属性值的,这就使得不同应用的Activity组件可以共享同一个affinity值或是让一个应用中的Activity组件拥有不同的affinity值。这样做只是在以下两种场合的时候才会有效:

一: intent对象中的flag属性被设定为FLAG_ACTIVITY_NEW_TASK,可以理解intent请求对象本身要求被激活的Activity以一个新task的根Activity的方式被启动。

二: manifest文件中的activity元素的allowTaskReparenting被设定为"true",这可以理解为被激活的Activity自身是允许进入其他task堆栈的。Reparent==重定父级(当某个task的 affinity属性值和这个Activity实例的affinity属性值一样的task堆栈走到前台时候,允许这个Activity实例进入这个前台堆栈中)

The FLAG_ACTIVITY_NEW_TASK--以新task方式触发Activity flag
    As described earlier, a new activity is, by default, launched into the task of the activity that called startActivity(). It's pushed onto the same stack as the caller. However, if the Intent object passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to house the new activity. Often, as the name of the flag implies, 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可以用intent对象激活另外一个Activity,默认场合这个被触发的Activity将加入前一个Activity所属的task堆栈中,但是一旦说startActivity()方法的参数:intent对象中的flag属性被设定为 FLAG_ACTIVITY_NEW_TASK,那么Android系统将为这个被激活的Activity查找拥有同样affinity的task,如果找到,该Activity将被压入找到的task 的堆栈中,如果找不到那么为这个Activity创建一个新task,并压入新task的堆栈。换句话说无论如何,这个Activity是不会进入到当期 task堆栈中的。

The allowTaskReparenting--允许进入其他task堆栈(重定父级) attribute
    If an activity has its allowTaskReparenting attribute set to "true", it can move from the task it starts into the task it has an affinity for when that task comes to the fore. 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 affinity) and it allows reparenting. One of your activities starts the weather reporter, so it initially belongs to the same task as your activity. However, when the travel application next comes forward, the weather reporter will be reassigned to and displayed with that task.

翻译:如果一个被激活的Activity的allowTaskReparenting属性被设定为"true",这个Activity先是进入当前task堆栈。但是如果拥有同一个affinity的其他某个task来到前台的时候,该Activity可以从自己所属的task堆栈中移动到前台绑定的这个task堆栈中。也就是说这个Activity实例随时都可能离开这个堆栈进入到另外一个堆栈中。

If an .apk file contains more than one "application" from the user's point of view, you will probably want to assign different affinities to the activities associated with each of them.

翻译:以上两种场合都涉及到task的affinity值,使用task的affinity值和Activity的affinity值的目的是为了细化task,如果说你的apk文件中包含用户视觉感官的多个“应用”,那么你可能就为你的activities设定不同的affinities,这样做的目的是为了满足应用程序需要应对多个task堆栈轮番出现在前台的场合。

     我们可以做这样的总结:通常一个应用中的activities是拥有同样的affinity值的,应用与用户交互过程中产生的activities实例都将属于同一个task堆栈,但是如果说你的应用足够复杂的话,那么很可能用户在视觉感官上产生这个应用是由多个应用构成的,这时候就需要把你的应用划分为不同逻辑意义上的task,假定需要三个不同逻辑意义上task,那么你这个应用中的activities元素的affinities属性可以相应设定为三类不同的值,如果是这样,那么该应用在Android操作系统上运行的过程将会出现三个不同的task堆栈,这三个task堆栈将轮番出现在前台。

你可能感兴趣的:(android)