Callback on AlertDialog I am using an AlertDialog in Android, asking the user for some input. I would like do run some code when the user has finished entering her input.

Since an AlertDialog is asynchronous, my code is not waiting for it. Therefore, I decided to use a callback, as in the following sample code:

This is the interface: AlertDialogCallback

public interface AlertDialogCallback&lt;T&gt;
{
    public void alertDialogCallback(T ret);
}


The main activity implements the interface and calls the function creating the alert dialog:

public class MainActivity extends Activity implements AlertDialogCallback&lt;String&gt;

...

public void alertDialogCallback(String ret)
{
    ...
}

...

askForInput(this, this);


And the function creating the dialog has a final reference to the class that implements AlertDialogCallback (here MainActivity). It has to be final because of the nested anonymous class.

public static void askForInput(Context context, final AlertDialogCallback&lt;String&gt; callback)
{
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setTitle("Enter input");
    alert.setMessage("Please enter some input");

    // Set an EditText view to get user input
    final EditText input = new EditText(context);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int whichButton)
        {
            String value = input.getText().toString();

            callback.alertDialogCallback(value);
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int whichButton)
        {
            // Canceled.
        }
    });

    alert.show();
}


I would like to know if this is a bad design (and why), and also if there is some kind of design pattern that is commonly used for this purpose in Android(/Java?).
Indeed the recommended approach for this (or at least what I would recommend) is to use callback interfaces.

The most important aspect though is designing this interface. In this case you should be sure that you're passing everything you need. However, I think you can rename the method to onOK or okPressed or similar. You might also consider returning a boolean to determine whether or not the dialog should really be closed (the input might need to be validated before it is accepted?) (there are ways to stop an AlertDialog from closing, you can find more information in earlier StackOverflow questions).



Other comments:

You should use String resources properly, this will separate the code from the text neatly. Use the resource id's instead.

alert.setTitle(R.string.alertInputTitle);
alert.setMessage(R.string.alertInputMessage);


Some strings are actually included in Android by default, such as android.R.string.ok and android.R.string.cancel, use them when possible.

When you want the negative button to just close the dialog, you can actually pass null as the DialogInterface.OnClickListener.

alert.setNegativeButton(android.R.string.cancel, null);