'Android nested AlertDialog - is this possible?
I am trying to ask the user for confirmation twice before I do something irreversible to the database. The problem is that the outer click handler does not wait for the inner click handler. Once the Yes button is clicked on the first dialog, the 2nd dialog is displayed briefly, but the outer handler executes and completes nonetheless, ultimately destroying both dialogs.
new AlertDialog.Builder(ActivityMain.this).setMessage(
"Are you sure?").setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
new AlertDialog.Builder(ActivityMain.this).setMessage(
"Are you really sure?").setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
....
Why is that?
Solution 1:[1]
I believe it's because dialogs are not blocking. As soon as they are displayed, processing moves on to the next line of code. The dialog is still displayed though, awaiting user interaction.
Solution 2:[2]
Another way is to close the first dialog in the first click handler with
arg0.dismiss();
then add
.show()
in your instantiation of the second dialog
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John |
| Solution 2 | Tiiso Agyei |
