简体   繁体   中英

Clear activity stack before launching another activity

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

I think you are looking for the FLAG_ACTIVITY_CLEAR_TOP flag to be used with your intents.

From the documentation at the link above, an example is provided:

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

You can see more about Tasks and the Backstack in Android at this link in the Android Dev Docs

您可以将标志用于新任务和清除任务,以从堆栈中清除所有先前的活动

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

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 in onDestroy of every activity.

if (isFinishing()) {
    finishActivity(REQUEST_CODE);
}

For example: You have A, B, C, D on the stack, call finishActivity(REQUEST_B) from A , this will chain the onDestroy calls so B kills C and C kills D .


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM