从Android应用程序堆栈中手动删除活动 [英] Remove activities manually from Android app stack

查看:187
本文介绍了从Android应用程序堆栈中手动删除活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Android原生应用程序,我试图做的是:

I been working on Android Native App , What i was trying to do is :

Activities - A -> B -> C  Then  A-> B -> C -> C  . 

从C活性,如果它再次指向C,那么我想从堆栈中手动删除C,B。 在我看来,那只能移动到。

From C Activity if it again point to C then i want to remove C , B from stack manually . On my back it should move only to A .

我试图完成(),但问题是:

I tried finish() but problem is :

Activities - A -> B ->  C  Then  A-> B -> C -> C  on finish A -> B -> C  required state A-> C .

有没有人知道如何赶在堆栈中的所有活动,并从堆栈中删除特定的活动??

Is anyone know how to catch all activities in stack and remove specific activities from stack ??

推荐答案

在活动℃,重写 onBack pressed 并添加类似:

In Activity C, override onBackPressed and add in something like:

@Override
public void onBackPressed() {
    if (shouldGoBackToA) {  // There are various ways this could be set
        Intent intent = new Intent(this, AActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        finish();
    }
}

FLAG_ACTIVITY_CLEAR_TOP 将导致它的堆栈去开始一个新的向下的活动现有的复印件。 <一href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP">From该文档:

FLAG_ACTIVITY_CLEAR_TOP will cause it to go down the stack to the existing copy of A Activity instead of starting a new one. From the docs:

公共静态最终诠释 FLAG_ACTIVITY_CLEAR_TOP
  如果已设置,以及正在启动的活性在当前任务已经运行,则代替启动该活动的一个新实例,所有的在其上的其他活动将被关闭,此意图将被传递到(现在的上顶部)老年活动作为一个新的意图。

public static final int FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

这篇关于从Android应用程序堆栈中手动删除活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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