There are so many questions on SO related to this, but no such direct questions I could find.
My app has a main activity M, and a sub activity S.
M launchMode
is singleTask
because I only ever want one instance running and I want S to exist in the same task as M.
S launchMode
is standard
, because it can have many instances.
In S, there is an Up button which must always take the user to M.
There are two types of task stack (with S on top):
- MSSSS...S
If the user presses Up in S, then the stack should become:
M (where M should be in the same state the user left it)
- SSSS...S
If the user presses Up in S, then the stack should become:
M (a new instance)
--
So how to construct the Intent
for the Up button...
In the first case, FLAG_ACTIVITY_CLEAR_TOP
should do the trick, but if we apply this to the second case then the user is dumped back in Home.
In the second case, FLAG_ACTIVITY_CLEAR_TASK
will do the trick, but if we apply this to the first case, we discover that M is finished and restarted.
One might also be tempted to try FLAG_ACTIVITY_NEW_TASK
, but then we find hitting Back from M will take the user to the screen where he/she hit Up.
Side note: how is this possible when M is singleTask
and the activities all have the same (default) task affinity?
So how to correctly construct the Up Intent
in this case?