- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
正如标题所说,为什么 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 标志:
startActivityForResult()
启动 C。B.afterActivityResult()
中,完成 B 并启动 A。关于android - 为什么 FLAG_ACTIVITY_CLEAR_TOP 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718356/
我想知道,2个代码有什么区别? newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
我被困在使用 Flag activity clear top usage 中。这对我来说是个大问题。有时有效,有时无效。我不关心是什么。这真的让我很困惑。请帮忙 在这里,我从 Login Acitiv
我遇到了 FLAG_ACTIVITY_CLEAR_TOP 问题。当用户启动应用程序时,会出现一个屏幕供他们登录或注册。用户登录到应用程序后,我希望关闭所有以前的 Activity 。当我按下后退按钮时
我想在我的所有 Activity 设备上都有一个主页按钮,以便它们可以将您带到起点。我从堆栈溢出和 http://developer.android.com/guide/topics/fundamen
我有一个简单的用例。我的 Activity 如下所示:A > B > C 现在,我收到推送通知。 我在我的 GCMIntentService 类中执行以下操作: 在onMessage中, Intent
我有 4 个 Activity A、B、C、D、E,每个 Activity 都声明为 android:launchMode="singleInstance",A 是 B 的 parentActivit
我真的不明白所有这些标志是如何工作的,因为我完全按照文档所说的去做,但它并没有解决我的问题。我的 Activity 是这样启动的:(第一)Activity A -> Activity B -> (第二
在我正在处理的应用程序中,Chrome 选项卡用于外部 OAuth 流。 在流程结束时,自定义选项卡保持打开状态并显示一条信息性消息,并发送一封包含自定义方案链接的电子邮件。 直到 Android 1
我有一个 Activity A,它使用 startActivityForResult 启动一个新 Activity B。 Activity B 使用 FLAG_ACTIVITY_CLEAR_TOP 重
在我的菜单中有一些项目。 Home 是它的一个项目,我想成为我的应用程序的根,每当用户点击它时,Android 清除堆栈然后返回主屏幕。 这是我的代码: menu.setOnItemClickedLi
根据上图,我有一些疑问。请按顺序阅读每个步骤:- 每个 block 都是一个 Android Activity 箭头代表 Stack Direction——Activity 打开的顺序(A 在应用程序
我的申请流程: 登录->个人资料->更新个人资料->ChangePass 我所有的 Activity 都扩展了 FragmentActivity 当我在 ChangePass Activity 中按下
我有多个管理连接的 Activity (B => C => D)。如果该连接断开,它们应该全部清除并将结果返回给 A,具体取决于原因(RESULT_USER_TERMINATED、RESULT_LOW
我有一个 MainActivity(它有 launchMode=singleTop),我从它转到其他 Activity ,例如 B 和 C。现在,我想在 B 和 C 中单击某个按钮时导航回 MainA
我的 Activity 堆栈是 A B C,C 在顶部。 A 使用 startActivityForResult() 启动了 B。 现在,在 C 中,它启动 A 并使用以下代码清除顶部:
正如标题所说,为什么 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 或 intent.setFlags(Intent.FLAG_ACTIVITY_CL
谁能用非常简单的方式解释一下 FLAG_ACTIVITY_CLEAR_TOP 是什么意思?我知道有很多关于它的问题,但没有一个答案让我满意。有人还可以举一个这个标志有用的例子吗?谢谢。 最佳答案 详情
我的应用程序涉及跟踪车辆通过检查门的移动 当在门口张贴的人员点击车辆号码上的“通过”按钮时,该车辆应从列表中删除。我通过再次调用 VehicleActivity 类并将门 id 放入键值对中来完成此操
我有一个像这样的 Activity 堆栈 A -> B -> C。我正在启动 Activity A,并从 C 清除顶部。现在发生的情况是它将立即完成 C,然后它将恢复 A,然后大约 2 秒后它会完成
我有很多 Activity ,比如登录->主->a、b、c、d->注销 [a、b、c、d 是四个 Activity ]。当我登录应用程序时,它会转到主程序并完成登录 Activity 。和 main
我是一名优秀的程序员,十分优秀!