I have read through Google Android Developer page, but the concept of task (http://developer.android.com/guide/components/tasks-and-back-stack.html) really confuse me.
After I read until SingleTask and SingleInstance, I am getting more confused.
I would like to ask some question by using examples, hope that I will have better understanding from these question:
Let's say I have 2 applications A and B, A has x, y, z activities; B has 1, 2, 3 activities:
Assume that their launch mode are Standard (Not using any intent flag). And x is Main Activity of app A; 1 is Main Activity of app B.
1) Launch app A, then x-> y -> 1, press home button, launch app A again, we will see activity y or 1?
2) Launch app A then x -> 1 -> y -> 2 -> z -> 3, press home button, launch app A, it will contain all the activities (x -> 1 -> y -> 2 -> z -> 3), or it contains x -> y -> z only? How about if we launch app B now? What activities will app B contain?
Now let's say activities 1, 2, 3 are SingleTask; x,y,z still Standard:
3) Launch app A, then x -> y -> 1 -> 2, press home button, launch app A, it will contain x -> y only or it contains x -> y -> 1 -> 2? How about if we launch app B now? app B will contain 1 only or 1 -> 2?
4) Launch app B, then 1 -> 2 -> 3 -> 1, will 2 and 3 be destroyed?
5) Launch app B, then 1 -> 2 -> 3, press home button, launch app A now, then x -> y -> 2 then press back button to drop 2. Launch app B now, what activities it contains? 1 -> 3 only or 1 -> 2 -> 3?
Thanks anyone reply and help!
You will see activity
1. You have a single task that containsx->y->1with activity1on the top of the activity stack in that task. When you press HOME, this task is moved to the background. When you launch the app again, Android finds the task stack and just brings it (intact) back to the foreground, showing you the top activity on the stack (in this case1).As above, you have a single task. When you press HOME, the task contains
x->1->y->2->z->3and is moved to the background. When you launch App A again, the task is brought forward (intact) and you will see activity3on top.Well, the question as such is not correct. What you really want to know is "What activities will this task contain?", but here is the answer:
If you launch App B from the HOME screen, you will start a new task. This task will contain a single activity, namely
1. This task has nothing to do with the other task (which is still in the background). The fact that the background task contains activities from 2 different applications is irrelevant.At the point where activity
ylaunches activity1, this will create a new task. So you will have one task containing activityx->yand a second task containing1. When activity1launches activity2what happens depends on more than just thelaunchModeof the activities. Even if activity2is declaredlaunchMode="singleTask", if thetaskAffinityof activity2is the same as thetaskAffinityof activity1(which, by default, it is, if they belong to the same application) then activity2will be created in the same task as activity1(ie: it will behave as if activity2hadlaunchMode="standard"). However, if activity1and activity2have differenttaskAffinity, then activity2will be launched as the root activity in a new task. Now you will have 3 tasks, like this: Task1 containsx->y, Task2 contains1and Task3 contains2.As above, this depends on
taskAffinity. If thetaskAffinityof activity1and2are the same, launching app B from the HOME screen will bring the task containing1->2to the foreground. If thetaskAffinityof the activities is different, launching app B from the HOME screen will bring the task containing activity1to the foreground.No.
2and3will not be destroyed. Assuming that1,2and3all havelaunchMode="singleTask"then it (again) depends on thetaskAffinitysettings. Assuming all activities have the sametaskAffinity, then you will have a single task containing1->2->3->1(you will have 2 instances of activity1) becausetaskAffinitytrumpslaunchMode.If all activities has different
taskAffinity, then after1->2->3you will have 3 separate tasks, each containing a single activity. Then, when activity3starts activity1, this will just bring the task containing activity1to the foreground and will not create a new instance of activity1.Again, this depends on
taskAffinity. If all activities of app B have the sametaskAffinitythen after1->2->3you will have one task. User presses the HOME button this task will go to the background. Now user launches app A creating a new task. Afterx->ythe second task contains these 2 activities. Now activityystarts activity2. Since this activity haslaunchMode="singleTask"and has a differenttaskAffinityfrom the other activities in the task (they all havetaskAffinityof App A), Android will create a new task with activity2as the root. Android cannot use the existing task containing1->2->3because that task does not contain activity2as its root. When the user presses BACK in2, this will finish activity2which will finish the third task, returning the user to the second task containingx->ywith activityyon top. Now pressing HOME and launching app B will bring the existing first task containing1->2->3to the foreground.However, if all the activities of app B have different
taskAffinity, then after1->2->3you will have 3 separate tasks each containing a single activity. User presses HOME and launches App A creating a new task (now you have 4 tasks). Afterx->ythe fourth task contains these 2 activities. Now activityystarts activity2. Android simply brings the task containing activity2to the foreground. User presses the BACK button, this finishes activity2and the task it was in (because that task is now empty), returning the user to the previous task which is the one containingx->yfrom app A. Launching App B from the HOME screen will simply bring the task containing activity1to the foreground. You now have 3 tasks: Task1 contains activity1and is in the foreground, Task2 contains activity3and is in the background, Task3 containsx->yand is in the background.NOTES
I realize this is complicated. My answer is from my head, I have not attempted to actually implement all these combinations and check (however, I have implemented many of these cases in the past and I do know how it really works). The reason is that most of what you described would not be done in the real world, so the examples are only theoretical and not practical. In real life you hardly ever need to use the
singleTaskorsingleInstancelaunch modes unless you are building your own HOME-screen replacement or you need to carefully control the way your application behaves when it is started by other applications. In most cases you will never have more than one activity which has a launch mode ofsingleTaskorsingleInstance.If you use
singleInstanceorsingleTaskyou need to be aware of howtaskAffinityworks and you also need to make sure that you have a different app icon (and probably also app label) for each activity that is declared as 'singleTask' or 'singleInstance'. If not, the user will not be able to return to the correct task due to the way these are shown in the list of recent tasks.