Hi there (my first question here ;-)
There might be similar questions but none of them seem to answer my question or gives me a real solution...
Problem
A
(root) starts B
. B
can start other activities. A
decides to dismiss the stack (network connection loss) and start C
. This seems to be a drastic decision but the user would expect that and that really makes sense in this case...
Question
How to do that? I would expect using a certain flag or just call something like dismissStack
. But I can't find the right solution (see research below).
Research
I read a lot of documentation but the available flags don't match my needs (or I didn't understand it right). Other answers out there tell me to remember the started activities (e.g. in an array) and finish them when needed. But this seems dirty to me and somehow I can't accept that to be the right solution. There must be something cleaner I hope?!
(Storing the stack seems even wrong to me. Your app can be killed by the system, so your static data, but your history will be loaded again on startup. So you would have to persist your saved stack. This gets even dirtier...)
Update
My code and what I have tried (abstracted):
// in A
startActivityForResult(new Intent(this, B.class), REQUEST_B);
// network fail in A
// setting any flags on C has no influence
startActivityForResult(new Intent(this, C.class), REQUEST_C);
(FLAG_ACTIVITY_CLEAR_TOP is no solution, please read the documentation...)
I press Back on C
and B
still pops up and is so still there...
I can remove the activity by calling finishActivity(REQUEST_B)
. But when B
started another activity D
it will still pop up if I hit Back on C
later. B
is gone but D
is still there...
I'm sure there is something I didn't understand yet or just missed. Can someone give me a hint or approve my research if it has to be really that way...
That's what I got so far and it works pretty good:
Use
startActivityForResult
. Create a dummy request code if you actually don't need to wait for a result. Then do this inonDestroy
of every activity.For example: You have
A, B, C, D
on the stack, callfinishActivity(REQUEST_B)
fromA
, this will chain theonDestroy
calls soB
killsC
andC
killsD
.That's the best solution I can think of. Other solutions out there are messing with the activity lifecycle and/or are error prone.
I still hope there's a cleaner way...