Android:如何检测“后退”按钮是否会退出应用程序(即这是堆栈上留下的最后一个活动)?

发布于 2024-12-06 09:51:36 字数 186 浏览 3 评论 0 原文

我想警告用户如果后按将完成堆栈上的最后一个活动,从而退出应用程序。我想弹出一个小面包并在接下来的几秒钟内检测到第二次后按,然后调用 finish()

我已经使用 onBackPressed() 编写了后按检测代码,但我找不到明显的方法来查看后堆栈上还剩下多少活动。

谢谢。

I'd like to warn the user if the back press is going to finish the last activity on the stack, thereby exiting the app. I'd like to pop up a little toast and detect a 2nd back press within the next few seconds and only then call finish().

I already coded the back press detection using onBackPressed(), but I can't find an obvious way to see how many activities are left on the back stack.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

流年已逝 2024-12-13 09:51:36

reddit is fun 应用程序执行此操作 通过重写 onKeyDown 方法

public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) {
        //Ask the user if they want to quit
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.quit)
        .setMessage(R.string.really_quit)
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Stop the activity
                finish();    
            }
        })
        .setNegativeButton(R.string.no, null)
        .show();

        return true;
    }
    else {
        return super.onKeyDown(keyCode, event);
    }
}

The reddit is fun app does this by overriding the onKeyDown method:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) {
        //Ask the user if they want to quit
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.quit)
        .setMessage(R.string.really_quit)
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Stop the activity
                finish();    
            }
        })
        .setNegativeButton(R.string.no, null)
        .show();

        return true;
    }
    else {
        return super.onKeyDown(keyCode, event);
    }
}
贪了杯 2024-12-13 09:51:36

droid-fu 库通过检查正在运行的任务堆栈并查看下一个任务是否是安卓主屏幕。请参阅 https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/activities/BetterActivityHelper.java

但是,我只会将这种方法用作最后的手段,因为它非常老套,无法在所有情况下工作,并且需要额外的权限才能获取正在运行的任务列表。

The droid-fu library does this by checking the stack of running tasks and seeing if the next task is the android home screen. See handleApplicationClosing at https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/activities/BetterActivityHelper.java.

However, I would only use this approach as a last resort since it's quite hacky, won't work in all situations, and requires extra permissions to get the list of running tasks.

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