Search code examples
androiddialogconfirmation

Android Confirmation dialog returning true or false


It seems to be there is no easy way to get an Alert dialog to return a simple value.
This code does not work (the answer variable cannot be set from within the listener, in fact it does not even compile)

public static boolean Confirm(Context context) {
    boolean answer;
    AlertDialog dialog = new AlertDialog.Builder(context).create();
    dialog.setTitle("Confirmation");
    dialog.setMessage("Choose Yes or No");
    dialog.setCancelable(false);
    dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int buttonId) {
            answer = true;
        }
    });
    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int buttonId) {
            answer = false;
        }
    });
    dialog.setIcon(android.R.drawable.ic_dialog_alert);
    dialog.show();
    return answer;
}

NOTE: It is important that the method is self contained, i.e., it does not depend on variables or constructs external to it. Just call it and get your answer, true or false.

So, what to do? This simple wish of returning true or false seems to be much more complicated than it deserves.

Also, the setButton method has the form:

dialog.setButton(int buttonId, String buttonText, Message msg)

But it is not clear how to use it, where is the meesage sent to, to whom, which handler is used?


Solution

  • Well, I was going to say that I am very pleased with myself because I found a simple answer, all by myself!
    But the truth is that although I find a way to return a value (which I show below). It is of no use.

    The real problem is I wanted a synchronous Dialog, a dialog that waits for the user to answer before resuming your code after dialog.show().
    There is no such beast in Android. All dialogs are asynchronous, so dialog.show() only posts the dialog in some queue (I think) and continues. Thus you don't get your answer in time.

    For all its worth (nothing) below you'll find how to set a value inside the method that builds the dialog. Maybe there are other uses for this technique, not related to the dialog lifecycle.




    To give some related info, I'll say that if you replace

    boolean answer;
    

    with

    final boolean answer;
    

    it is possible to access the variable from within the listener, but it is not possible to assign it a new value, since it was declared as final.

    Here comes the trick.
    Define the variable as:

    final boolean[] answer = new boolean[1];
    

    Some of you already see why this will work. The final variable here is not the single element of the boolean array, is the array itself.
    So now you can assign the array element [0] as you wish.

    dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int buttonId) {
            answer[0] = true;
        }
    });
    . . .
    return answer[0];
    

    And finally you can return it from your method.