清除旧版 SDK 上的完整 Android 活动堆栈(缺少 FLAG_ACTIVITY_CLEAR_TASK)

发布于 2024-10-19 11:40:01 字数 496 浏览 1 评论 0原文

我已经完成了一些阅读和搜索,但找不到清除当前活动堆栈的方法。我的应用程序的上下文是由后台服务/通知启动的活动。

想象一下,我的应用程序允许您组织人员列表。几个小时前,您正在“查看”活动中查看人员 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伊面 2024-10-26 11:40:01

只是把他们全部杀掉

您可以通过使用 BroadcastReceivers 来实现这一点:

  • 创建一个 BaseActivity,如下所示:

public class BaseActivity extends GuiceActivity {
    private KillReceiver mKillReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mKillReceiver = new KillReceiver();
        registerReceiver(mKillReceiver,
            IntentFilter.create("kill", "spartan!!!"));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mKillReceiver);
    }
    private final class KillReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }
}

  • 让您的活动扩展该 BaseActivity
  • 每当你想清除堆栈时:

Intent intent = new Intent("kill");
intent.setType("spartan!!!");
sendBroadcast(intent);

Just kill'em all!

You can achieve that by using BroadcastReceivers:

  • Create a BaseActivity like this:

public class BaseActivity extends GuiceActivity {
    private KillReceiver mKillReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mKillReceiver = new KillReceiver();
        registerReceiver(mKillReceiver,
            IntentFilter.create("kill", "spartan!!!"));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mKillReceiver);
    }
    private final class KillReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }
}

  • Make your activities extend that BaseActivity.
  • Whenever you want to clear the stack:

Intent intent = new Intent("kill");
intent.setType("spartan!!!");
sendBroadcast(intent);

油焖大侠 2024-10-26 11:40:01

如果您的应用程序主目录已在堆栈中运行,则当活动 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 flag FLAG_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.

梦太阳 2024-10-26 11:40:01

获取一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文