Remove activities manually from Android app stack(从 Android 应用程序堆栈中手动删除活动)
问题描述
我一直在开发 Android Native App,我想做的是:
I been working on Android Native App , What i was trying to do is :
Activities - A -> B -> C Then A-> B -> C -> C .
如果 C Activity 再次指向 C,那么我想手动从堆栈中删除 C、B.在我的背上,它应该只移动到 A .
From C Activity if it again point to C then i want to remove C , B from stack manually . On my back it should move only to A .
我试过finish(),但问题是:
I tried finish() but problem is :
Activities - A -> B -> C Then A-> B -> C -> C on finish A -> B -> C required state A-> C .
有谁知道如何捕获堆栈中的所有活动并从堆栈中删除特定活动??
Is anyone know how to catch all activities in stack and remove specific activities from stack ??
推荐答案
在 Activity C 中,重写 onBackPressed
并添加如下内容:
In Activity C, override onBackPressed
and add in something like:
@Override
public void onBackPressed() {
if (shouldGoBackToA) { // There are various ways this could be set
Intent intent = new Intent(this, AActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
finish();
}
}
FLAG_ACTIVITY_CLEAR_TOP
将导致它沿堆栈向下移动到 A Activity 的现有副本,而不是启动一个新副本.来自文档:
FLAG_ACTIVITY_CLEAR_TOP
will cause it to go down the stack to the existing copy of A Activity instead of starting a new one. From the docs:
public static final int FLAG_ACTIVITY_CLEAR_TOP
如果设置,并且正在启动的活动已经在当前任务中运行,那么不会启动该活动的新实例,而是关闭它上面的所有其他活动,并且此 Intent 将被传递到(现在顶部)作为新意图的旧活动.
public static final int 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.
这篇关于从 Android 应用程序堆栈中手动删除活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Android 应用程序堆栈中手动删除活动
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01