在 Android 中按下后退按钮时保持我的活动

发布于 2024-12-18 19:08:13 字数 1275 浏览 0 评论 0原文

我正在实现这个应用程序,当按下后退键时我需要暂停它或显示一个对话框来询问用户是否真的想退出。

我确实覆盖了后退键以显示带有 2 个按钮的对话框,是并取消,但活动无论如何都已完成,而没有显示对话框

我用来覆盖后退键的代码如下

// Overriding The Back Key To Stop The Music If Running
@Override
public void onBackPressed() {

    super.onBackPressed();
    show_are_you_sure_exit();
}

,函数如下

public void show_are_you_sure_exit() {
    final AlertDialog show_are_you_sure_delete = new AlertDialog.Builder(
            this).create();
    show_are_you_sure_delete
            .setMessage("Are You Sure You Want To Exit?! Your Pregress Won't Be Saved");
    show_are_you_sure_delete.setButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface StopActivityDialog,
                        int which) {
                    start_main_screen_intent();
                    finish();
                }
            });
    show_are_you_sure_delete.setButton2("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });

    show_are_you_sure_delete.show();

}

: start_main_screen_intent( )开始另一项活动,

感谢您的帮助:)

I'm implementing this application where i need to pause it when the back key is pressed or show a dialog to ask the user if he really wants to exit.

I did override the back key to show a dialog with 2 button, yes and cancel, but the activity is finished anyways without showing the dialog

the code I am using to override the back key is the following

// Overriding The Back Key To Stop The Music If Running
@Override
public void onBackPressed() {

    super.onBackPressed();
    show_are_you_sure_exit();
}

and the function is the following

public void show_are_you_sure_exit() {
    final AlertDialog show_are_you_sure_delete = new AlertDialog.Builder(
            this).create();
    show_are_you_sure_delete
            .setMessage("Are You Sure You Want To Exit?! Your Pregress Won't Be Saved");
    show_are_you_sure_delete.setButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface StopActivityDialog,
                        int which) {
                    start_main_screen_intent();
                    finish();
                }
            });
    show_are_you_sure_delete.setButton2("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });

    show_are_you_sure_delete.show();

}

with start_main_screen_intent() starts another activity

thx for your help :)

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

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

发布评论

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

评论(3

谜泪 2024-12-25 19:08:13

您正在调用 super.onBackPressed() 来调度事件并终止活动
你想要的是

@Override
public void onBackPressed() {
    show_are_you_sure_exit();
}

这样的方式 你以你自己的方式处理事件,而不是正常的方式

your are calling super.onBackPressed() which dispatches the event up and kills the activity
what you want is

@Override
public void onBackPressed() {
    show_are_you_sure_exit();
}

this way YOU handle the event your way, not the normal way

﹂绝世的画 2024-12-25 19:08:13

尝试删除 super.onBackPressed() 调用。在这种情况下,您可能还需要删除 @Override,否则 Eclipse 可能会对您大喊大叫。我认为正在发生的事情是基础实施正在结束该活动。

Try removing the super.onBackPressed() call. You may also need to remove the @Override in this case or Eclipse might yell at you. I think what is happening is the base implementation is ending the activity.

_蜘蛛 2024-12-25 19:08:13

不要调用 super.onBackPressed();。它所做的唯一事情(如果您扩展Activity)就是调用finish()

然而,请注意这个“退出”功能并非来自 Android 世界。我建议检查您的应用程序架构以摆脱退出概念。

您可以在这篇文章中找到有关“退出”为何不好的详细信息:退出应用程序会让人皱眉吗?

Don't call super.onBackPressed();. The only thing it does (if you extend Activity) is it calls finish().

However, note this 'exit' feature is smth not from Android world. I'd recommend to review your app architecture to get rid of exit concept.

You can find details on why 'exit' is bad in this post: Is quitting an application frowned upon?

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