android taskaffinity的使用 [英] Use of android taskaffinity

查看:83
本文介绍了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(由任务中根ActivitytaskAffinity确定).但是,如果新的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆