The affinity indicates which task an activity prefers to belong to.
By default, all the activities from the same application have an affinity for each other.
So, by default, all activities in the same application prefer to be in the same task.
However, you can modify the default affinity for an activity.
Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different task affinities.
You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.
The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that name to identify the default task affinity for the application.
The affinity comes into play in two circumstances:
1.When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.
A new activity is, by default, launched into the task of the activity that called startActivity().
It's pushed onto the same back stack as the caller.
However, 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.
If this flag causes an activity to begin a new task and the user presses the Home button to leave it, there must be some way for the user to navigate back to the task.
Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always put FLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity().
If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started, such as with a launcher icon (the root activity of the task has a CATEGORY_LAUNCHER intent filter; see the Starting a task section below).