Clear activity stack before launching another activity

自古美人都是妖i 提交于 2019-12-11 04:06:19

问题


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


回答1:


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




回答2:


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




回答3:


You can use both the flags for new task and clear task to clear all previous activities from the stack

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


来源:https://stackoverflow.com/questions/7796400/clear-activity-stack-before-launching-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!