android - Activity onBackPress 不显示警报对话框

标签 android android-alertdialog

我有一个 Activity ,当我按下后退按钮时,它不会显示警报对话框。 可能是什么问题呢? 这是我的代码

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(LogFish.this);

                // set title
                alertDialogBuilder.setTitle("Exit");
                alertDialogBuilder.setIcon(R.drawable.ic_action_search);

                // set dialog message
                alertDialogBuilder
                    .setMessage("This action will cause you to abandon all changes on current new fish log. \n\nAre you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("YES",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, close
                            startActivity(new Intent(LogFish.this,MainActivity.class));
                            finish();

                        }
                      })
                    .setNegativeButton("NO",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close

                            dialog.cancel();
                        }
                    });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();

                    }

最佳答案

你必须删除 super.onBackPressed();

关于android - Activity onBackPress 不显示警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14041045/

相关文章:

android - 如何在 ViewPager 中正确使用 fragment ?

java - 嵌套的 Java 接口(interface)

android - 按下后退按钮时完成 Activity (警报对话框)

java - alertdialog - removeView 必须被调用

android - 拥有 'release' apk 后,我应该删除 firebase 中的 SHA1 调试 key 吗?

android - 向数据库插入记录后如何更新 ListView ?

java - EditText 上的 TextChanged

android - AlertDialog 的 setCancelable(false) 方法不起作用

android - 使用android中的警报框弹出窗口从gridview更新数据库

android - 无法从警报对话框中的 EditText View 获取数据