我想在android设备的后退按钮上显示提示消息,询问用户是否确定他的天气。如果用户按“是”,那么他应该导航到前一个。在“否”的情况下,应恢复活动。但是,当我按下按钮它执行这两个操作时会发生问题。它也导航到以前的活动,并显示警报对话框..这是我的代码..看看并指导我..在此先感谢。显示按下后退按钮上的提醒对话框
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
alertdialog.setTitle("Warning");
alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(secAddition.this,addition.class);
startActivity(intent);
secAddition.this.finish();
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=alertdialog.create();
alertdialog.show();
}}
不要调用startActivity上BackPress。你应该调用超级OnBackPress。当按下硬件后退时。检查我的答案。 –