Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Task Affinity Explanation

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?

like image 965
Traxex1909 Avatar asked Jul 26 '13 04:07

Traxex1909


People also ask

What is Flag_activity_clear_top?

flag — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being started is already running in the current task then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed (with call to onDestroy method) and this intent is delivered to the resumed instance of the Activity (now ...

What is Android launchMode?

This is the default launch mode of activity (If not specified). It launches a new instance of an activity in the task from which it was launched. Numerous instances of the activity can be generated, and multiple instances of the activity can be assigned to the same or separate tasks.

What are intent flags in Android?

Use Intent Flags Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.

Which attribute is used to define affinity of an activity?

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".


1 Answers

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

like image 194
Eric Leschinski Avatar answered Sep 22 '22 22:09

Eric Leschinski