Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Behaviours are different ?- android:launchMode="singleTask" , android:taskAffinity="" And Intent.FLAG_ACTIVITY_NEW_TASK

I have Four Activity - A,B,C,D

I am calling these four activity in manner --> A-B-C-D-B. (Specified Manner)

I have three scenario.

1st :- I am defining android:launchMode="singleTask" only in B Activity. And I am calling all activity via Intent in above specified manner.

Now First calling A-B-C-D , BackStack Task 1 : A-B-C-D,

Now I again call B, Then BackStack Task 1 : A-B. Here C and D Activities are destroyed.

2nd :- I am defining android:launchMode="singleTask" & android:taskAffinity="" in B Activity. And I am calling all activity via Intent in above specified manner.

Now First calling A-B-C-D , BackStack Task 1 : A

                                  Task 2 : B-C-D

Now I again call B, Then BackStack Task 1 : A

                                Task 2 : B ,Here C and D Activities are destroyed.

3rd :- I am defining Intent.FLAG_ACTIVITY_NEW_TASK & android:taskAffinity="" in B Activity. And I am calling all activity via Intent in above specified manner.

Now First calling A-B-C-D , BackStack Task 1 : A

                                  Task 2 : B-C-D

Now I again call B, Then BackStack Task 1 : A

                                Task 2 : B-C-D , Here **Can't call B again**

And here It says FLAG_ACTIVITY_NEW_TASK produces the same behavior as the "singleTask" - https://developer.android.com/guide/components/activities/tasks-and-back-stack.html

So Which are correct scenarios? I am getting it right or I am misunderstanding something.

like image 594
zephyr Avatar asked Sep 26 '17 13:09

zephyr


People also ask

What is Flag_activity_new_task in Android?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .

What is true about flag activity new task?

flag — FLAG_ACTIVITY_CLEAR_TASK: This clears any existing task that would be associated with the Activity before the Activity is started. This Activity then becomes the new root of the task and old Activities are finished. It can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.


1 Answers

I've reproduced the behaviour as you've described.

The first scenario is basically undocumented because a singleTask Activity should always be the root of a task. Because Android uses taskAffinity when determining how and where to launch an Activity, your singleTask Activity is being launched into the existing task, but not as the root Activity. This makes the behaviour different from the behaviour that is documented. When you launch B from D, Android needs to deliver the Intent to B in a call to onNewIntent(), which it cannot do with C and D sitting on top of B. So Android finishes C and D and then calls onNewIntent() on B.

The second scenario shows that you now have 2 tasks, because the singleTask Activity is launched into a new task as the root Activity. Again, when D launches B, Android needs to deliver the Intent to B by calling onNewIntent(), which it cannot do if C and D are in the way. So Android finishes C and D and then delivers the Intent to B in onNewIntent().

The third scenario shows the use of FLAG_ACTIVITY_NEW_TASK. When A launches B with FLAG_ACTIVITY_NEW_TASK, Android looks for an existing task that has B as the root Activity. Since it doesn't find one, it launches B in a new task as the root Activity. When D launches B with FLAG_ACTIVITY_NEW_TASK, Android looks for a task that has B as the root Activity, and if it finds one, it brings that task to the foreground but does not deliver the Intent to onNewIntent(). This is just a way to bring an existing task to the foreground in the state that it was in when it went to the background. This is what you are seeing. This behaviour is also documented (sort-of), but due to the very complex interdependencies between all of these scenarios, all possible cases are not explicitly documented. Sometimes you need to discover how this works by empirical observation instead of reading the documentation.

Thanks for the challenge.

like image 196
David Wasser Avatar answered Oct 13 '22 11:10

David Wasser