我有一個活動,當我按回按鈕,然後它不顯示警報對話框。 可能是什麼問題? 這裏是我的代碼活動onBackPress不顯示警報對話框
@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();
}
請檢查你的背部鑰匙沒有的onkeydown捕獲().... –
檢查,如果你不崩潰,因爲你的主線程上運行? – IamStalker
事實上,它的調用,但立即你的活動完成,因爲你寫了'super.onBackPressed();'。如果你刪除這個,那麼你的活動沒有完成後面的按下並顯示你的對話框。 – user370305