110

What exactly is the attribute taskAffinity used for? I have gone through the documentation but I couldn't understand much.

Can anyone explain task affinity in laymans terms?

Marian Paździoch
  • 7,671
  • 9
  • 49
  • 88
Traxex1909
  • 2,450
  • 4
  • 17
  • 23

3 Answers3

165

What is Android Task Affinity used for?

An android application has Activities that form a stack like a deck of cards. If you start an android application, and start five activities A,B,C,D,E. They will form a stack

E   - chat view
D   - weather screen
C   - map view
B   - weather screen
A   - login screen

E was the last Activity to be started and it is showing. If you close E, D will be shown. If you close D, C will be shown. etc.

Notice that Activities B and D are the same activity. What if the user were to make some modifications to the D weather screen, and then decided to close the activity, then close the C Map view?

Then the user would be back at the weather screen and the user would be unhappy because the changes made at level D weather screen were not saved in level B weather screen. Although it's the same activity, it's a different STATE of that activity.

The user had a 5 layer stack of activities, and two of them were the same activity. Popping all 5 off the stack will create the phenomenon where you will be interacting with two different versions of the same activity and can be quite confusing.

Users don't usually think in terms of a rigid stack of activities. They think: "ooh the weather view I'll make a change there" and then they want to go back to the Map view. Then back up again because they want to go back to the Login screen. Why is the B weather app showing and why didn't it save the settings from level D?

The programmer might be able to alleviate some confusion if Activities B and D were linked in state. That way changes to one changes the other. Each time the user opens up a new weather screen, it secretly opens the single instance of the weather screen.

In these circumstances, changing the taskAffinity of the Activity might be desirable. The user would change level D. Then back up to level B. And see the changes in B that were made to D.

The program keeps a stack you can backup through, which is nice, and when the user opens up X instances of the same activity in random places, they are all one.

Slideshow with more explanation: http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
  • 15
    How about if use the `singleInstance` instead of using the `taskAffinity`? – hguser Oct 30 '13 at 06:13
  • singleInstance has disadvantage that an Activity being singleInstance cannot have any other activity in activity stack. with taskAffinity you may configure that one activity will start in different task, and another in current. – Malachiasz Feb 07 '14 at 14:03
  • 1
    In some cases, SingleInstance is the right way to go. specially if you want to make sure that your activity is the only activity in its task, and that it is the root *regardless* of the intent that created it. This way you can expose that activity to 3rd party devs and apps without losing control of the activity's task record. – Ran Nachmany Jun 02 '14 at 07:09
  • 5
    In the case described above, I would prefer using Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as a flag in the Intent calling Activity D, this would bring Activity B on top of all the others and keep only one instance of it – EyalBellisha Jun 02 '14 at 09:31
  • 72
    Actually, this answer hardly touches on `taskAffinity` at all :-( The explanation is vague. Amazing that theses answers get so many upvotes. – David Wasser Aug 02 '16 at 09:58
  • down as this is stupid: consider stack CHAT, CHAT,CHAT, LIST OF CHATS (tree same activities with tree different states) – ceph3us Aug 27 '16 at 14:07
  • 7
    So long answer but still doesn't mention the simple fact: `taskAffinity` is used to specify the name of the task the activity prefers to run in. When an `Intent` contains the flag `FLAG_ACTIVITY_NEW_TASK`, the activity is then put into this particular task (defined by `taskAffinity`). – Stan Mots Jan 14 '17 at 16:42
  • 5
    `Although it's the same activity, it's a different STATE of that activity.` I would say, "It's **another instance** of that activity." – Maksim Dmitriev Jan 19 '17 at 19:27
4

The affinity indicates which task an activity prefers to belong to.

The affinity comes into play in two circumstances:

When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.

When an activity has its allowTaskReparenting attribute set to "true".

Please refer http://developer.android.com/guide/components/tasks-and-back-stack.html

hitesh141
  • 947
  • 10
  • 25
-1

You can find all cases (and some times edge cases) in this detailed presentation

Please, refer Manipulating Android tasks and back stack

Cypres
  • 1
  • 2