Android 为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23718356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:22:29  来源:igfitidea点击:

Why does FLAG_ACTIVITY_CLEAR_TOP not work?

androidandroid-intentandroid-activity

提问by ashu

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

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

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

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

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

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

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

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

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

I have also tried using setFlags().

我也试过使用setFlags().

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

我在 SO 上阅读了关于这个问题的不同问题,但我找不到正确的答案。>_<

Somebody please help!

有人请帮忙!

Edit

编辑

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

根据@codeMagic 的要求,活动“A”中的 onBackPressed() 代码。

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

回答by matiash

From the documentation for FLAG_ACTIVITY_CLEAR_TOP:

来自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.

如果设置,并且正在启动的活动已经在当前任务中运行,那么不是启动该活动的新实例,而是将关闭它之上的所有其他活动,并且此 Intent 将被传递到(现在在顶部)旧活动作为新意图。

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.

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

As I see it, you have two options here:

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

1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASKflags. 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.

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

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

2)另一种可能的解决方案(如果前面的假设不正确)是根本不使用意图标志:

  • 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.
  • B 以 C 开头startActivityForResult()
  • C 完成而不是调用 A,而是为 B 设置了一个结果,表明必须启动 A。
  • 在 中B.afterActivityResult(),完成 B 并启动 A。

回答by goodKode

You are missing the Intent.FLAG_ACTIVITY_SINGLE_TOPflag

你错过了Intent.FLAG_ACTIVITY_SINGLE_TOP国旗

Try this:

尝试这个:

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

回答by Rod_Algonquin

You used a diferrent intent: use the one you initialized:

您使用了不同的意图:使用您初始化的意图:

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

solution:

解决方案:

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \RIGHT;;

You could either put a noHistory true to the Activity A in the manifest

您可以在清单中为活动 A 设置 noHistory

android:noHistory=true