android taskaffinity的使用 [英] Use of android taskaffinity
问题描述
我正在阅读关于taskaffinity&使用以下活动创建了一个演示应用程序:
I am reading about taskaffinity & created a demo app with following Activities :
- A
- B ===> taskaffinity,com.ando
- C
- D ===> taskaffinity,com.ando
- E
据记载,具有相同taskaffinity的Activity会秘密打开另一个实例的单个实例.
It is written that, Activities with same taskaffinity secretly opens the single instance of another one.
因此,我将每个活动的登录名放在onResume上以查看任务ID.如果它创建单个实例,那么为什么当我打开D时它不执行B的onResume,反之亦然.
So, I put log in onResume of every activity to see task id. If it creates single instance then why its not executing onResume of B when I open D and vice-versa.
我阅读了开发人员网站和其他文章,但仍然不知道如何使用taskaffinity及其用途,为什么我们不应该使用singleInstance呢?
I read developers site and other post but still not got how to use taskaffinity and whats its use, why we should'tn use singleInstance instead ?
清单:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.danroid.taskaffinity.A"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- android:taskAffinity="com.ando" -->
<activity
android:name="com.example.danroid.taskaffinity.B"
android:label="@string/app_name"
android:taskAffinity="@string/task_affinity" >
</activity>
<activity
android:name="com.example.danroid.taskaffinity.C"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.danroid.taskaffinity.D"
android:label="@string/app_name"
android:taskAffinity="@string/task_affinity" >
</activity>
<activity
android:name="com.example.danroid.taskaffinity.E"
android:label="@string/app_name" >
</activity>
</application>
推荐答案
当您调用startActivity()
从一个Activity
过渡到另一个Activity
时,如果未在Intent
标志中设置Intent.FLAG_ACTIVITY_NEW_TASK
,则新不管taskAffinity
的值如何,Activity
都将在同一任务中启动.
When you call startActivity()
to transition from one Activity
to another, if you do not set Intent.FLAG_ACTIVITY_NEW_TASK
in the Intent
flags, the new Activity
will be started in the same task, regardless of the value of taskAffinity
.
但是,如果在Intent
标志中设置了Intent.FLAG_ACTIVITY_NEW_TASK
,则新的Activity
将仍然在同一任务中启动,如果新的Activity
具有相同的taskAffinity
作为任务的taskAffinity
(由任务中根Activity
的taskAffinity
确定).但是,如果新的Activity
具有不同的taskAffinity
,新的Activity
将在新任务中启动.
However, if you set Intent.FLAG_ACTIVITY_NEW_TASK
in the Intent
flags, the new Activity
will still be started in the same task if the new Activity
has the same taskAffinity
as the taskAffinity
of the task (this is determined by the taskAffinity
of the root Activity
in the task). But, if the new Activity
has a different taskAffinity
, the new Activity
will be started in a new task.
根据您的描述,如果在启动新的Activity
时未设置Intent.FLAG_ACTIVITY_NEW_TASK
,则所有活动都将以同一任务结束.
Based on your description, if you don't set Intent.FLAG_ACTIVITY_NEW_TASK
when starting a new Activity
, then all of your activities will end up in the same task.
这篇关于android taskaffinity的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!