你如何使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 来清除 Activity Stack? [英] How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?

查看:20
本文介绍了你如何使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 来清除 Activity Stack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇关于使用它的帖子,但肯定遗漏了一些东西,因为它对我不起作用.我的活动 A 的 launchmode=singleTop"在清单中.它以launchmode=singleInstance"启动活动B.Activity B 打开浏览器并接收并返回 Intent,这就是它是 singleInstance 的原因.我正在尝试覆盖后退按钮,以便用户返回到活动 A,然后可以按返回离开活动,而不是再次返回活动 B.

I've read through several posts about using this, but must be missing something as it's not working for me. My activity A has launchmode="singleTop" in the manifest. It starts activity B, with launchmode="singleInstance". Activity B opens a browser and receives and intent back, which is why it's singleInstance. I'm trying to override the back button so that the user is sent back to the activity A, and can then press Back to leave the activity, rather than back to activity B again.

// activity B
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
  && keyCode == KeyEvent.KEYCODE_BACK
  && event.getRepeatCount() == 0) onBackPressed();
 return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
 startActivity(new Intent(this, UI.class)
 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
 return;
}

从浏览器返回后,栈是...A,B,浏览器,B

After returning from the browser, the stack is... A,B,Browser,B

我希望这段代码将堆栈更改为...一种...以便再次按下返回键会将用户带回主屏幕.

I expect this code to change the stack to... A ... so that pressing back once more takes the user back to the Home Screen.

相反,它似乎将堆栈更改为...A,B,浏览器,B,A...好像那些标志不存在.

Instead it seems to change the stack to... A,B,Browser,B,A ...as though those flags aren't there.

我尝试在 startActivity 之后在活动 B 中调用 finish(),但是后退按钮再次将我带回浏览器!

I tried calling finish() in activity B after startActivity, but then the back button takes me back to the browser again!

我错过了什么?

推荐答案

@bitestar 有正确的解决方案,但还有一步:

@bitestar has the correct solution, but there is one more step:

它隐藏在文档中,但是您必须将 ActivitylaunchMode 更改为 standard 以外的任何内容.否则它会被销毁并重新创建而不是重置到顶部.

It was hidden away in the docs, however you must change the launchMode of the Activity to anything other than standard. Otherwise it will be destroyed and recreated instead of being reset to the top.

这篇关于你如何使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 来清除 Activity Stack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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