清除旧版 SDK 上的完整 Android 活动堆栈(缺少 FLAG_ACTIVITY_CLEAR_TASK)
我已经完成了一些阅读和搜索,但找不到清除当前活动堆栈的方法。我的应用程序的上下文是由后台服务/通知启动的活动。
想象一下,我的应用程序允许您组织人员列表。几个小时前,您正在“查看”活动中查看人员 X,该活动现在位于您的堆栈顶部。在将来的某个时刻,该服务会触发,我会为 Y 人弹出一个新的“通知”活动。从那里您可以编辑 Y 的详细信息。
当您完成此活动时,弹出堆栈并最终查看人员 X 将是一种令人困惑的用户体验。理想情况下,我想返回到用户正在执行的任何操作(电子邮件等...),或者至少返回到我的应用程序的家。
我尝试使用 FLAG_ACTIVTY_NEW_TASK
启动“通知”,但这似乎没有帮助:任务完成后,它只是返回到上一个任务。我想要的似乎是Android 3的新FLAG_ACTIVITY_CLEAR_TASK
,它在以前的SDK中不存在。
有人有建议来实现这一目标吗?
I've done qui a bit of reading and searching on SO, but can't find a way to clear the current activity stack. The context of my app is an activity started by a a background service / notification.
Imagine that my app allows you to organise a list of people. A few hours ago, you were viewing person X in the "View" activity, that's now the top of your stack. At some point in the future, the service triggers and I popup a new "Notify" activity for person Y. From there you can edit person Y's details.
When you finish this activity, it would be a confusing user experience to pop the stack and end up viewing person X. Ideally I'd like to go back to whatever the user was doing (email etc...), or at least to my app's home.
I tried starting "Notify" with FLAG_ACTIVTY_NEW_TASK
but that doesn't seem to help: when the task finishes it simply goes back to the previous task. What I want seems to be Android 3's new FLAG_ACTIVITY_CLEAR_TASK
, which doesn't exist in previous SDKs.
Does anyone have a suggestion to achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是把他们全部杀掉!
您可以通过使用
BroadcastReceivers
来实现这一点:BaseActivity
,如下所示:BaseActivity
。Just kill'em all!
You can achieve that by using
BroadcastReceivers
:BaseActivity
like this:BaseActivity
.如果您的应用程序主目录已在堆栈中运行,则当活动 Y 完成时,您可以使用
FLAG_ACTIVITY_CLEAR_TOP
标志启动应用程序主目录(使用startActivity()
)。由于它已经在堆栈中,因此不会创建它的新实例,而是返回应用程序的主目录并清除其顶部的堆栈。If you already have an instance of your app's home running in the stack, when activity Y finishes you could start your app's home (using
startActivity()
) with the flagFLAG_ACTIVITY_CLEAR_TOP
. As it's already on the stack, instead of creating a new instance of it, this would bring you back to your app's home and clear the stack on top of it.获取一个 ArrayList 并将所有活动对象保存到每个活动的 oncreate() 中的数组列表中。每当您想要完成特定活动时,只需从数组列表中检索该活动实例并完成即可。
Take a ArrayList and save all activities objects into arraylist in oncreate() of every activity. Whenever you want to finish particular activity ,just retrieve that activity instance from arraylist and finish that.