【发布时间】:2021-11-23 01:52:10
【问题描述】:
我的问题很简单,当我按下返回按钮时,它将转到上一个片段,我该怎么做?当我在对话中单击“是”按钮时,如果我单击没有进入前一个片段的按钮,它将完成显示..
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
【问题讨论】:
标签: android android-fragments modal-dialog android-alertdialog android-fragmentactivity