我在菜单活动中重写了
onBackPressed
函数,以便在用户从菜单返回时直接退出程序。但是,如果我已经通过其他活动,它不会退出;它会转到堆栈中的上一个活动,并且对话框框不会弹出,尽管它实际上出现了一秒钟就立即消失了。
这是我的onBackPressed
函数:
public void onBackPressed() {
// super.onBackPressed();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you Sure want to close the Application..?")
.setCancelable(false)
.setTitle("EXIT")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
//super.onBackPressed();
}