patternjavaMinor
Callback on AlertDialog
Viewed 0 times
callbackalertdialogstackoverflow
Problem
I am using an
Since an
This is the interface:
The main activity implements the interface and calls the function creating the alert dialog:
And the function creating the dialog has a
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?).
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:
AlertDialogCallbackpublic interface AlertDialogCallback
{
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
...
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 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?).
Solution
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
Other comments:
You should use String resources properly, this will separate the code from the text neatly. Use the resource id's instead.
Some strings are actually included in Android by default, such as
When you want the negative button to just close the dialog, you can actually pass
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);Code Snippets
alert.setTitle(R.string.alertInputTitle);
alert.setMessage(R.string.alertInputMessage);alert.setNegativeButton(android.R.string.cancel, null);Context
StackExchange Code Review Q#52351, answer score: 6
Revisions (0)
No revisions yet.