简体   繁体   English

清除 Android 回栈

[英]Clearing the Android back stack

I'm trying to do something similar to this question.我正在尝试做与 这个问题类似的事情。

I've got a main app A, and sub-apps B and C.我有一个主应用程序 A、子应用程序 B 和 C。 B is a searchable dictionary, and C is a definition, so if you search for many words, you can end up with the stack looking like A > B > C > B > B > C > B > C > C > C... etc B is a searchable dictionary, and C is a definition, so if you search for many words, you can end up with the stack looking like A > B > C > B > B > C > B > C > C > C.. 。 ETC

Whenever I go back, I'd like to go back to the original B, so I want the back stack to basically stay at A > B all the time.每当我 go 回来时,我想 go 回到原来的 B,所以我希望后面的堆栈基本上一直停留在 A > B。

I've currently got it set up using an Intent so when the back button is pressed from C, it goes to a new instance of B, but that's obviously not what I want.我目前已经使用 Intent 进行了设置,因此当从 C 按下后退按钮时,它会转到 B 的新实例,但这显然不是我想要的。

Ideas?想法?

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (splash.sdk < 5 && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }
    //This will make the back button exit the app to the home screen.
    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
        return;
    }

Shouldn't hitting BACK from C take you to the original B?不应该从 C 回击带你到原来的 B 吗? So the stack should look like:所以堆栈应该如下所示:

A>
  B>
    C<
  B>
    C>
      C<
    C<
  B<
A

with > going to the next activity, and < being the back button? > 进入下一个活动,而 < 是后退按钮?

You could override onBackPressed for all the activities, so for C it loads a new B, B it loads a new A, and A you would do moveTaskToBack(true);您可以为所有活动覆盖 onBackPressed,因此对于 C,它会加载一个新的 B,B 它会加载一个新的 A,而 A 您将执行moveTaskToBack(true); , but that's less of a solution and more of a hack to make it work. ,但这不是解决方案,而是使其工作的更多技巧。

Try to use onResume so that B comes up like it would on default.尝试使用 onResume 以使 B 像默认情况下一样出现。 Set any pertinent variables to what they are in a new activity.将任何相关变量设置为它们在新活动中的内容。 If you only want this when you're coming from C, have a class boolean that is set to true when C is loaded, and check it in onResume or onRestart. If you only want this when you're coming from C, have a class boolean that is set to true when C is loaded, and check it in onResume or onRestart. If it's true, set everything to default/blank, and set the boolean to false.如果为真,请将所有内容设置为默认/空白,并将 boolean 设置为假。 If not, load it how it was (this is if they hit home and come back to the app, basically).如果没有,请按原样加载(基本上,如果他们回家并回到应用程序)。 I'm not at my work desk, but I think you'll want:我不在办公桌前,但我想你会想要:

@Override
    public void onResume() {
        super.onResume();
        if(fromC == true){
        //Set all variables to load default dictionary
        fromC = false;
        }
    }
}

This should make the original B act like a new B.这应该使原来的 B 表现得像一个新的 B。

Or instead, you could do it the easy way, and when you call the intent to load a new B like this:或者,你可以用简单的方法来做,当你调用意图来加载一个新的 B 时,像这样:

startActivity(getIntent());
finish();

That'll make there only be one B. Making it load as a blank when you go back is a little bit different and requires class variables, or some other tricky trick that I am thinking of.这将使只有一个 B。当您返回 go 时将其加载为空白有点不同,并且需要 class 变量或我正在考虑的其他一些棘手的技巧。 This is something like what you'll want to do: .zip of small sample这类似于您想要做的事情:小样本的.zip

If you can get your code reworked to something like that almost (where you can use the appropriate variables to set up things to work right if that makes any sense).如果您可以将代码重新修改为几乎类似的东西(如果有意义的话,您可以在其中使用适当的变量来设置工作正常)。

You can set an Intent flag to do this.您可以设置一个Intent标志来执行此操作。 Say you are in Activity C and you want to call Activity B: you add a flag to the Intent called FLAG_ACTIVITY_CLEAR_TOP .假设您在Activity C 中,并且您想调用Activity B:您向Intent添加一个名为FLAG_ACTIVITY_CLEAR_TOP的标志。 So:所以:

class C extends Activity {

    //...

    private void gotob() {
        Intent go = new Intent(this, B.class);
        go.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(go);
    }

    //...

}

What this does is either to go to the top instance of B in the back stack (and clear everything else off the top), or create a new instance if none exists.这样做是将 go 到后堆栈中 B 的顶部实例(并清除顶部的所有其他内容),或者如果不存在则创建一个新实例。 I haven't worked out if there is a way to do this with launch flags though—there are some similar modes but I don't think any are identical.不过,我还没有弄清楚是否有办法用启动标志来做到这一点——有一些类似的模式,但我认为没有一个是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM