3

I want to know what is the default task affinity for singleInstance activity?

Since each singleInstance Activity opens up as root activity in a new task. and there is no need to declare android:taskAffinity.

See I have read the developer guides and docs for android activity.

https://developer.android.com/guide/components/activities/tasks-and-back-stack and https://developer.android.com/guide/topics/manifest/activity-element.html#aff

<activity android:name=".MainActivity"
                  android:launchMode="singleInstance"
        >
</activity>

I want to know the taskAffinity - string literal for that task in which singleInstance activity resides as root.

Yogesh Seralia
  • 340
  • 5
  • 23
  • taskAffinity is used to specify the name of the task the activity prefers to run in. When you use FLAG_ACTIVITY_NEW_TASK with intent, Activity is put into this task defined by taskAffinity. – Rajnish suryavanshi May 29 '19 at 03:34
  • yes @Rajnishsuryavanshi I agree with you, Since we know that if there is no affinity defined then the default affinity is equal to the package name defined in manifest. But in case of singleInstance activity, it by default starts in new task, then what will be value of affinity of this new task? that's my question. Hope it would help. – Yogesh Seralia May 29 '19 at 04:33
  • I would refer you please watch this video. This will help you. https://www.youtube.com/watch?v=gRub-Pm_A0Q&list=PLfuE3hOAeWhYCPPLA75AXfd0pILeyePjv&index=15 – Rajnish suryavanshi May 29 '19 at 04:55

2 Answers2

4

The default taskAffinity is the package name of the app from the manifest <package> tag.

This is for all activities, no matter what the launch mode is.

This is why a lot of developers have problems using the special launch modes singleInstance and singleTask, because taskAffinity trumps launch mode, so sometimes those activities will NOT be launched in a new task, but simply launched into the existing task as if the launch mode were "standard".

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    I am very confused when reading the Android document. I think it always create the new task but it did not work like that. It depends on the `taskAffinity`. Many thanks for your explanation, I did some check and you are completely correct. – RoShan Shan May 10 '22 at 16:15
3

The default taskAffinity of all activities including singleInstance activity will be the same (which is the applicationId), but the taskId of singleInstance activity will be different (unique) than other activities; but the users cannot see and switch tasks from the recents.

Setting a different taskAffinity to a singleInstance activity will give it that taskAffinity value and additionally allow the users to see and switch between the tasks from the recents.

Ashwin
  • 7,277
  • 1
  • 48
  • 70