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