如预期FLAG_ACTIVITY_CLEAR_TOP不起作用 [英] FLAG_ACTIVITY_CLEAR_TOP does not work as expected
问题描述
我有4个活动A,B,C,D,E,每个声明为的android:launchMode =singleInstance
和A为 parentActivity
B的,和B是C的父活动,清单中。
现在用户导航到E这样的:A> B> C> D> E。
而且我有在电子商务具有以下onClickListener一个按钮
意向意图=新意图(E.this,C.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(意向);
我需要什么?(也可根据文档,如果我的理解是正确的,这应该发生)
- 活动D和E应该从栈 清除
- 活动C必须恢复接收的意图(如singleInstance被设置,它不会被新创建的)
我想要这个堆栈:C | C | A
块引用>
这是怎么回事?
- 活动C被恢复接收的意图
- 活动D和E 不会从堆栈中清除。我可以一次又一次地点击返回按钮来查看E,D,B,A。
我得到这个堆栈:C | E |开发| C | A
块引用>
PS:我的问题是非常相似然而,这个时,给出的答案有不适合这个问题。一些答案,我发现那里和其他地方的:
- 我不能使用NEW_TASK标志,因为我需要保持A,B堆栈活着。
- 我无法在D使用startActivityForResult(),然后完成()它这实际上是一个黑客,因为我在E的其他决定性因素,例如根据用户的输入提供的意图,其他一些活动。
- 我无法完成()活动D,同时提供意向E,然后完成()电子同时击中C,这实际上解决这个问题。但是,如果后面是pssed第E $ P $,我希望它回到D和不是C。
- 当我,只要我做的尝试第E结束()
startactivity(意向)
设置标志后,它只是完成E,而不是D。
问题是在的android:launchMode =singleInstance
。根据本<一href=\"http://developer.android.com/intl/ru/reference/android/R.styleable.html#AndroidManifestActivity_launchMode\"相对=nofollow>为singleInstance文档:
如果这个活动试图启动一个新的活动,新的活动将
在一个单独的任务来展开。
块引用>所以,你的活动D的推出,新的任务,这就是为什么它没有被删除。
I have 4 activities A, B, C, D, E with each declared as
android:launchMode="singleInstance"
and A beingparentActivity
of B, and B being parent Activity of C, in manifest.Now user navigated to E like this: A > B > C > D > E. And I have a button in E with the following onClickListener
Intent intent = new Intent(E.this, C.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);
What I need? (Also according to documentation, if I understood it right, this should happen)
- Activities D and E should be cleared from stack
- Activity C should be resumed receiving the intent (as singleInstance is set, it will not be created newly)
I want this stack: C | B | A
What's happening?
- Activity C is resumed receiving the intent
- Activities D and E are NOT cleared from stack. I can click back button again and again to see E, D, B, A.
I get this stack: C | E | D | B | A
PS: My question is very similar to this however, the answers given there does not suit this question. Some answers I found there and elsewhere:
- I can't use NEW_TASK flag, as I need to keep A, B in stack alive.
- I can't use startActivityForResult() on D and then finish() it which in fact is a hack, as I have other decisive factors in E such as delivering the intent to some other activity depending on user input.
- I can't finish() activity D, while delivering intent to E, and then finish() E while hitting C, which would actually solve the problem. But, if back is pressed on E, i want it go back to D and not C.
- When I try finish() on E as soon as I do
startactivity(intent)
after setting flags, it just finishes E, but not D.
解决方案The problem is in
android:launchMode="singleInstance"
. According to the documentation for singleInstance:If this activity tries to start a new activity, that new activity will be launched in a separate task.
So, your activity D is launched as the new task, and that's why it is not deleted.
这篇关于如预期FLAG_ACTIVITY_CLEAR_TOP不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!