启动另一个活动之前清除活性栈 [英] Clear activity stack before launching another activity

查看:257
本文介绍了启动另一个活动之前清除活性栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好(我的第一个问题在这里; - )

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

问题的结果
A (根)启动 B B 可以开始其他活动。 A 决定驳回栈(网络连接丢失),并启动 C 。这似乎是一个剧烈的决定,但用户会期望和真正有意义在这种情况下...

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

问题的结果
怎么做?我希望用某种标志或只调用类似 dismissStack 。但我无法找到的右键的解决方案(参见下面的研究)。

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

更新结果
我的code和我都试过(抽象):

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 是无解的,请阅读文档...)

(FLAG_ACTIVITY_CLEAR_TOP is no solution, please read the documentation...)

我preSS <大骨节病>返回 C B 仍然弹出,如此仍然存在...

I press Back on C and B still pops up and is so still there...

我可以通过调用删除活动 finishActivity(REQUEST_B)。但是,当 B 开始另一个活动 D 它仍然会弹出如果我打<大骨节病>返回 C 之后。 B 是走了,但 D 还是有的...

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

推荐答案

这是我走到这一步,和它的作品pretty好:

That's what I got so far and it works pretty good:

使用 startActivityForResult 。创建一个虚拟请求code,如果你其实并不需要等待结果。然后,为此在的onDestroy 每一项活动的。

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);
}

例如:你有 A,B,C,D 在堆栈上,呼叫 finishActivity(REQUEST_B) A ,这将链中的的onDestroy 呼吁让 B 杀死 C C 杀死 D

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

这篇关于启动另一个活动之前清除活性栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆