Android - AlertDialog within onContextItemSelected does not work
-
27-09-2019 - |
Question
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!
Solution
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() :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow