清除整个历史堆栈,并开始在Android上一个新的活动 [英] Clear the entire history stack and start a new activity on Android

查看:493
本文介绍了清除整个历史堆栈,并开始在Android上一个新的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能启动活动在栈上,才清除全部历史?

Is it possible to start an activity on the stack, clearing the entire history before it?

情况

我有一个活动的堆栈,要么去A-> B-> C或B-> C(画面A选择的用户令牌,但许多用户只有一个令牌)。

I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token).

在画面C用户的可以的采取行动,这使得画面B无效的,所以应用程序要带他们到屏幕上,不管它是否已经在堆栈中。然后画面A应该是堆在我的应用程序的唯一项目。

In screen C the user may take an action which makes screen B invalid, so the application wants to take them to screen A, regardless of whether it is already in the stack. Screen A should then be the only item on the stack in my application.

备注

有许多其他类似的问题,但我还没有发现任何回答这个确切的问题。我打过电话的getParent()完成() - 这始终会导致空指针异常。 FLAG_ACTIVITY_CLEAR_TOP 只有当该活动已经在堆栈中。

There are many other similar questions, but I haven't found anything that answers this exact question. I tried calling getParent().finish() - this always results in a null pointer exception. FLAG_ACTIVITY_CLEAR_TOP only works if the activity is already on the stack.

推荐答案

在API级别11一个新的标志意向加入只是为了这个:<一href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK">Intent.FLAG_ACTIVITY_CLEAR_TASK

In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK

我想弄清楚,使用:

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

不幸的是,API拉特&LT; = 10,我还没有找到一个清晰的解决方案这一点。 该<一href="http://stackoverflow.com/questions/3473168/clear-the-entire-history-stack-and-start-a-new-activity-on-android/3473377#3473377">"DontHackAndroidLikeThis"解决方案确实是纯粹的两轮牛车。你不应该这样做。 :)

Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this. The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)

编辑: 根据@ 本·皮尔逊的注释,API&LT; = 10,现在你可以使用<一个href="http://developer.android.com/reference/android/support/v4/content/IntentCompat.html#FLAG_ACTIVITY_CLEAR_TASK">IntentCompat类相同。我们可以使用 IntentCompat.FLAG_ACTIVITY_CLEAR_TASK 标志清除任务。所以,你可以支持pre API级别11为好。

As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK flag to clear task. So you can support pre API level 11 as well.

这篇关于清除整个历史堆栈,并开始在Android上一个新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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