为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用? [英] Why does FLAG_ACTIVITY_CLEAR_TOP not work?

查看:22
本文介绍了为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,为什么 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 不起作用?

As the title says, Why intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) or intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) won't work?

我有 3 个活动,让我们说 A、B 和 C.

I have 3 Activities let us say A, B and C.

当我尝试使用代码从 C 启动 Activity A 时:

When I am trying to launch Activity A from C with code:

Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

它只是启动 Activity A,但不清除顶部.!-_-

It simply starts Activity A but doesn't clear the top.! -_-

我也尝试过使用 setFlags().

我在 SO 上阅读了有关此问题的不同问题,但找不到正确答案.>_<

I read different questions on SO about this problem, but I couldn't find the right answer. >_<

有人请帮忙!

编辑

@codeMagic 要求的活动A"中的 onBackPressed() 代码.

Code for onBackPressed() in activity 'A' as requested by @codeMagic.

@Override
public void onBackPressed(){
    if(wvLogin.canGoBack())
        wvLogin.goBack();
    else
        super.onBackPressed();
}

推荐答案

来自 FLAG_ACTIVITY_CLEAR_TOP 的文档:

如果设置,并且正在启动的活动已经在当前任务,而不是启动该任务的新实例活动,其上的所有其他活动将被关闭,并且此 Intent 将作为新意图.

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.

正如您在评论中添加的那样,活动 A 在调用 B 之前已经完成,因此这种情况不适用.将改为启动活动 A 的新实例.

As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.

在我看来,您有两个选择:

As I see it, you have two options here:

1) 使用 Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK 标志.这将启动活动 A 作为堆栈的根.它有效,但堆栈中的任何其他活动都将丢失.假设 A 是第一个活动(或者至少,您对任务堆栈中的任何先前活动不感兴趣),那么这无关紧要.注意:CLEAR_TASK 需要 API 级别 11.

1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.

2) 另一种可能的解决方案(以防前面的假设不成立)是根本不使用意图标志:

2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:

  • B 以 startActivityForResult() 开始 C.
  • C 完成而不是调用 A,而是为 B 设置了一个结果,表明必须启动 A.
  • B.afterActivityResult()中,完成B并启动A.
  • B starts C with startActivityForResult().
  • Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
  • In B.afterActivityResult(), finish B and launch A.

这篇关于为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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