0

I am trying to understand the need for task affinity in Android. I searched other SO answers as well.

I am particularly interested in the combination of a singleTask launch mode and task affinity.

For singleTask launch mode (OR Intent with NEW_TASK flag), system takes one of 3 actions:

  1. If activity exists, it will resume it
  2. If activity doesn't exist, it will look for a task with matching affinity to add the activity
  3. Without finding a matching task, system will create a new task with this activity as root

I kind of understand the need to the first case from this answer. It talks about having same activity in multiple states, and how it can create inconsistency problems in user experience.

What I am more puzzled about is the second case - why does the system need to find an existing task with the same affinity ? What kind of use case does this fulfill and would break if this feature is disallowed ? Why is it necessary ?

From this link:

... if the intent passed to startActivity() contains the
FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to
house the new activity. Often, 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 same link also talks about task reparenting. This is another feature I cannot understand. The link does give an example of a weather/travel app:

... 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 application affinity) and it allows 
re-parenting with this attribute. When one of your activities starts the weather 
reporter activity, it initially belongs to the same task as your activity. 
However, when the travel application's task comes to the foreground, the weather 
reporter activity is reassigned to that task and displayed within it.

My question for this feature is similar. I'm not able to tell whether this feature is to fulfill some necessary user experience requirement OR is just a fancy add-on to tasks ? Why do activities need to be re-parented across tasks ?

Can someone please help me answer the above two questions ?

Community
  • 1
  • 1
Jake
  • 16,329
  • 50
  • 126
  • 202

1 Answers1

1

I'll take a stab at this!

I believe Android/Google's intentions were to have a seamless interaction for the user.

So when I read about affinity what came to mind were URLs, emails, documents and such. Does it make sense to ask the user what application to open especially if they already have one of the applications open that can handle the intent.

Same goes for re-parenting. The task opens another application but what happens when the user is finished with that task and wants to go back to the original application? In the user's perspective it is one experience regardless of how many applications are needed to meet that experience.

(I swear I read about this in a Material Design doc from Android....)

zec
  • 294
  • 1
  • 9