Android - AlertDialog within onContextItemSelected does not work
-
27-09-2019 - |
문제
I have a following pseudo-code.
public boolean onContextItemSelected(MenuItem aItem) {
switch(aItem.getItemId()) {
case A: {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete")
.setMessage("Delete?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do stuff A...
}
});
// do stuff B...
return true;
}
}
}
The problem is that it never shows the alert dialog. However, it does things as stated in "do stuff B..."
Does anyone know why AlertDialog is now showing?
Thank you!
해결책
you need to .create() .show() will solve the problem :)
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete")
.setMessage("Delete?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.show();
And when you don't need it anymore you can dismiss() it.
Edit: sorry. forgot .create() :)
제휴하지 않습니다 StackOverflow