我的应用程序中有两个警告对话框。
1用于退出应用程序,另一个用于重定向到url。
我在对话框中显示了对话框。
弹出的第一个对话框是重定向url,当用户按下它时会重定向到url。
但是当用户按下后退按钮时,我必须将其关闭,并且只想在后退按钮上单击时显示退出对话框。
这是我的代码。
boolean flag = false;
@Override
public void onBackPressed() {
if (flag) {
super.onBackPressed();
} else {
final AlertDialog.Builder alert = new AlertDialog.Builder(
MainActivity.this);
alert.setMessage("EXIT"); // Message
// here
alert.setPositiveButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
AlertDialog.Builder exitdialog = new AlertDialog.Builder(
MainActivity.this);
exitdialog
.setMessage("EXIT");
exitdialog.setTitle(R.string.app_name);
exitdialog.setIcon(R.drawable.logo);
exitdialog.setPositiveButton("NO",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
});
exitdialog.setNegativeButton("Yes",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
/*
* Intent i=new
* Intent(getApplicationContext
* (),MainActivity.class);
* startActivity(i);
*/
finishFromChild(getParent());
getIntent()
.setFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
}
});
AlertDialog alertDialog = exitdialog.create();
alertDialog.show();
}
});
alert.setNegativeButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Intent viewTwitter = new Intent(
"android.intent.action.VIEW",
Uri.parse("URL"));
startActivity(viewTwitter);
overridePendingTransition(R.anim.fadein,
R.anim.fadeout);
}
});
final AlertDialog alertDialog = alert.create();
alertDialog.show();
}
答案 0 :(得分:0)
删除finish();
默认操作dismiss
将dialog.dismiss();
放在onClick
方法
修改强>
设置flag = true;
boolean flag = false;
@Override
public void onBackPressed() {
if (flag) {
super.onBackPressed();
} else {
final AlertDialog.Builder alert = new AlertDialog.Builder(
MainActivity.this);
alert.setMessage("EXIT"); // Message
// here
alert.setPositiveButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
AlertDialog.Builder exitdialog = new AlertDialog.Builder(
MainActivity.this);
exitdialog
.setMessage("EXIT");
exitdialog.setTitle(R.string.app_name);
exitdialog.setIcon(R.drawable.logo);
exitdialog.setPositiveButton("NO",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
});
exitdialog.setNegativeButton("Yes",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
/*
* Intent i=new
* Intent(getApplicationContext
* (),MainActivity.class);
* startActivity(i);
*/
finishFromChild(getParent());
getIntent()
.setFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
}
});
AlertDialog alertDialog = exitdialog.create();
alertDialog.show();
}
});
alert.setNegativeButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
flag= true;
Intent viewTwitter = new Intent(
"android.intent.action.VIEW",
Uri.parse("URL"));
startActivity(viewTwitter);
overridePendingTransition(R.anim.fadein,
R.anim.fadeout);
}
});
final AlertDialog alertDialog = alert.create();
alertDialog.show();
}