我有一个活动,当我按下返回按钮时,它没有显示警报对话框。可能是什么问题呢?这是我的代码
@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();
}