Android task and process, SingleTask and SingleInstance -


i have read through google android developer page, concept of task (http://developer.android.com/guide/components/tasks-and-back-stack.html) confuse me.

after read until singletask , singleinstance, getting more confused.

i ask question using examples, hope have better understanding these question:

let's have 2 applications , b, has x, y, z activities; b has 1, 2, 3 activities:

assume launch mode standard (not using intent flag). , x main activity of app a; 1 main activity of app b.

1) launch app a, x-> y -> 1, press home button, launch app again, see activity y or 1?

2) launch app x -> 1 -> y -> 2 -> z -> 3, press home button, launch app a, contain activities (x -> 1 -> y -> 2 -> z -> 3), or contains x -> y -> z only? how if launch app b now? activities app b contain?

now let's activities 1, 2, 3 singletask; x,y,z still standard:

3) launch app a, x -> y -> 1 -> 2, press home button, launch app a, contain x -> y or contains x -> y -> 1 -> 2? how if launch app b now? app b contain 1 or 1 -> 2?

4) launch app b, 1 -> 2 -> 3 -> 1, 2 , 3 destroyed?

5) launch app b, 1 -> 2 -> 3, press home button, launch app now, x -> y -> 2 press button drop 2. launch app b now, activities contains? 1 -> 3 or 1 -> 2 -> 3?

thanks reply , help!

assume launch mode standard (not using intent flag). , x main activity of app a; 1 main activity of app b.

1) launch app a, x-> y -> 1, press home button, launch app again, see activity y or 1?

you see activity 1. have single task contains x->y->1 activity 1 on top of activity stack in task. when press home, task moved background. when launch app again, android finds task stack , brings (intact) foreground, showing top activity on stack (in case 1).

2) launch app x -> 1 -> y -> 2 -> z -> 3, press home button, launch app a, contain activities (x -> 1 -> y -> 2 -> z -> 3), or contains x -> y -> z only?

as above, have single task. when press home, task contains x->1->y->2->z->3 , moved background. when launch app again, task brought forward (intact) , see activity 3 on top.

how if launch app b now? activities app b contain?

well, question such not correct. want know "what activities task contain?", here answer:

if launch app b home screen, start new task. task contain single activity, namely 1. task has nothing other task (which still in background). fact background task contains activities 2 different applications irrelevant.

now let's activities 1, 2, 3 singletask; x,y,z still standard:

3) launch app a, x -> y -> 1 -> 2, press home button, launch app a, contain x -> y or contains x -> y -> 1 -> 2?

at point activity y launches activity 1, create new task. have 1 task containing activity x->y , second task containing 1. when activity 1 launches activity 2 happens depends on more launchmode of activities. if activity 2 declared launchmode="singletask", if taskaffinity of activity 2 same taskaffinity of activity 1 (which, default, is, if belong same application) activity 2 created in same task activity 1 (ie: behave if activity 2 had launchmode="standard"). however, if activity 1 , activity 2 have different taskaffinity, activity 2 launched root activity in new task. have 3 tasks, this: task1 contains x->y, task2 contains 1 , task3 contains 2.

how if launch app b now? app b contain 1 or 1 -> 2?

as above, depends on taskaffinity. if taskaffinity of activity 1 , 2 same, launching app b home screen bring task containing 1->2 foreground. if taskaffinity of activities different, launching app b home screen bring task containing activity 1 foreground.

4) launch app b, 1 -> 2 -> 3 -> 1, 2 , 3 destroyed?

no. 2 , 3 not destroyed. assuming 1, 2 , 3 have launchmode="singletask" (again) depends on taskaffinity settings. assuming activities have same taskaffinity, have single task containing 1->2->3->1 (you have 2 instances of activity 1) because taskaffinity trumps launchmode.

if activities has different taskaffinity, after 1->2->3 have 3 separate tasks, each containing single activity. then, when activity 3 starts activity 1, bring task containing activity 1 foreground , will not create new instance of activity 1.

5) launch app b, 1 -> 2 -> 3, press home button, launch app now, x -> y -> 2 press button drop 2. launch app b now, activities contains? 1 -> 3 or 1 -> 2 -> 3?

again, depends on taskaffinity. if activities of app b have same taskaffinity after 1->2->3 have 1 task. user presses home button task go background. user launches app creating new task. after x->y second task contains these 2 activities. activity y starts activity 2. since activity has launchmode="singletask" , has different taskaffinity other activities in task (they have taskaffinity of app a), android create new task activity 2 root. android cannot use existing task containing 1->2->3 because task not contain activity 2 root. when user presses in 2, finish activity 2 finish third task, returning user second task containing x->y activity y on top. pressing home , launching app b bring existing first task containing 1->2->3 foreground.

however, if activities of app b have different taskaffinity, after 1->2->3 have 3 separate tasks each containing single activity. user presses home , launches app creating new task (now have 4 tasks). after x->y fourth task contains these 2 activities. activity y starts activity 2. android brings task containing activity 2 foreground. user presses button, finishes activity 2 , task in (because task empty), returning user previous task 1 containing x->y app a. launching app b home screen bring task containing activity 1 foreground. have 3 tasks: task1 contains activity 1 , in foreground, task2 contains activity 3 , in background, task3 contains x->y , in background.

notes

i realize complicated. answer head, have not attempted implement these combinations , check (however, have implemented many of these cases in past , know how works). reason of described not done in real world, examples theoretical , not practical. in real life hardly ever need use singletask or singleinstance launch modes unless building own home-screen replacement or need control way application behaves when started other applications. in cases never have more 1 activity has launch mode of singletask or singleinstance.

if use singleinstance or singletask need aware of how taskaffinity works , need make sure have different app icon (and app label) each activity declared 'singletask' or 'singleinstance'. if not, user not able return correct task due way these shown in list of recent tasks.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -