gpt4 book ai didi

android - 为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用?

转载 作者:可可西里 更新时间:2023-11-01 18:49:47 32 4
gpt4 key购买 nike

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

我有 3 个 Activity ,让我们说 A、B 和 C。

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

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

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

我也尝试过使用 setFlags()

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

有人请帮忙!

编辑

@codeMagic 请求的 Activity “A”中的 onBackPressed() 代码。

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

最佳答案

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

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

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

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

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

  • B 使用 startActivityForResult() 启动 C。
  • C 没有调用 A,而是完成了,为 B 设置了一个结果,表明必须启动 A。
  • B.afterActivityResult() 中,完成 B 并启动 A。

关于android - 为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718356/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com