Android 开发文档 程序基础——Affinities and new tasks

一般来说,一个程序中的所有activity互相之间都有关系affinity ,也就是说,他们比较有可能属于同一个任务。 但是,可以利用<activity>元素的taskAffinity 属性来设置每个activity之间单独的关系。

不同程序中定义的activity可以共享一个affinity,同一个程序中的activity可以标志几个不同的affinity。 affinity在两个不同的情况下开始起作用:当运行一个activity的Intent对象含有 FLAG_ACTIVITY_NEW_TASK标记,当一个activity的 allowTaskReparenting设置为true。

如前所述,一个新的activity往往运行在叫做 stratActivity的activity的task中,然后就放在调用者所在的stack上。但是,如果转到startActivity()的 Intent对象含有 FLAG_ACTIVITY_NEW_TASK 标记,系统就会找一个不同的task来放置新的activity。正如标记名字所表达的,往往就是一个新的task。但是,这也并不是必须的。如果已经有 一个已经存在的task,与新的activity有同样的 affinity,新的activity就会运行在这个task中。如果没有的 话,就开一个新的task。

如果一个activity的allowTaskReparenting 属性被设置为true,那它就可以从它开始的task中移动到与它有关系的task中,当这个task转到前台时(这个task应该是指与 activity有关系的,译者buhutuu.cn 注)。 例如一个报告被选择城市的天气的activity被定义为一个旅行程序的一部分。它就有了与同一个程序中其他activity相同的affinity,并 且允许 reparenting。你的activity中的一个启动了天气报告,它初始就和你的activity在同一个task中。但是, 当旅行程序运行到前台,天气报告将会再分配给这个任务。

如果一个apk中有包含不止一个程序(从用户的角度来看),那么你最好给每一个activity分配不同的affinity。

 

转自我的android博客

 

原文

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

The FLAG_ACTIVITY_NEW_TASK flagAs 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.The allowTaskReparenting attributeIf an activity has its allowTaskReparenting attribute set to “true “, it can move from the task it starts in to 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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值