Android弹出AlertDialog onBackPressed

10

我正在尝试完成应用程序中的主菜单。我认为在OnBackPressed方法中添加一个AlertDialog会是个简单又好看的点缀。然而,出于某种原因,我遇到了各种错误。

我在OnBackPressed方法中创建了AlertDialog并显示它,但当我按下返回按钮时,应用程序会直接关闭,并显示窗口泄漏的错误信息。

有什么办法可以解决这个问题吗?我已经搜索了大约30分钟,但找不到其他人遇到类似问题的情况。


展示 onBackPressed() 的代码...虽然我不确定你想做什么是个好主意。 - Squonk
1
当按下返回按钮时显示的AlertDialogs(即“确认退出”对话框)会严重减慢用户体验。我以前做过这个,当我在后续版本中删除它们时,非常惊讶于我的应用程序看起来快了多少。除非你真的、真的、真的需要,否则不要使用它们 :)。 - Alex Lockwood
@AlexLockwood,你认为如果用户简单地返回,否则用户所做的更改将丢失,我真的,真的,真的需要显示警报对话框吗? - winklerrr
4个回答

40

尽量不要调用 super.OnBackPressed(),可以使用以下代码:

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    MyActivity.this.finish();
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();

}

7

如果您想调用super.onBackPressed(),请使用以下代码:

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //finish();
                    MyActivity.this.onSuperBackPressed();
                    //super.onBackPressed();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
    /*
    if (handleCancel()){
        super.onBackPressed();
    }
    */
}

public void onSuperBackPressed(){
    super.onBackPressed();
}

我刚刚在MyActivity中添加了一个名为onSuperBackPressed的新公共方法,并使用super方法。

感谢K_Anas提供了这个绝妙的想法。


5
我在OnBackPressed中创建了AlertDialog并显示,但当我按下返回按钮时,应用程序会直接关闭,并出现窗口泄漏的错误提示。
如果在你的OnBackPressed方法中调用了super.OnBackPressed(),那么应用程序将会被关闭,因为这是OnBackPressed方法的基本实现。不要调用super方法,这样应用程序就不会关闭。

0

在你的对话框中调用 yourActivity.this.onBackPressed()

utility.makeAlertDialog("Cancel Verification","Do you want to cancel the verification process","Cancel Verification",new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which) {
            utility.failureToast("OTP verification");
            Intent intent = new Intent(MobileOTPActivity.this,MobileNumberLoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
            MobileOTPActivity.this.onBackPressed();
        }
    },"Close");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接