Tôi có một Hoạt động trong đó khi tôi nhấn nút quay lại thì hộp thoại cảnh báo không hiển thị. Điều gì có thể là vấn đề? Đây là mã của tôiHoạt động trênBackPress không hiển thị hộp thoại cảnh báo
@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();
}
Vui lòng kiểm tra nếu chủ chốt trở lại của bạn không bị bắt trong onKeyDown() .... –
Kiểm tra nếu bạn không sụp đổ vì bạn chạy trên các chủ đề chính? – IamStalker
Thực ra nó được gọi nhưng ngay lập tức Hoạt động của bạn hoàn thành vì bạn đã viết 'super.onBackPressed();'. Nếu bạn xóa thao tác này thì Hoạt động của bạn chưa hoàn thành khi quay lại và hộp thoại của bạn được hiển thị. – user370305