What is the "correct" way to return the values to the calling activity from a complex custom dialog - say, text fields, date or time picker, a bunch of radio buttons, etc, plus a "Save" and "Cancel" button?
Some of the techniques I've seen on the web include:
public data members in the Dialog-derived class which can be read by the Activity
public "get" accessors . . . " . . " . . "
Launching the dialog with an Intent (as opposed to show() ) plus handlers in the Dialog class which take input from the various controls and bundle them up to be passed back to the Activity so when listener hits "Save" the bundle is passed back using ReturnIntent()
Listeners in the Activity which process input from the controls that are in the dialog e.g., so the TimePicker or DatePicker's listeners are really in the Activity. In this scheme practically all the work is done in the Activity
One Listener in the Activity for the "Save" button and then the Activity directly interrogates the controls in the dialog; the Activity dismisses the dialog.
...plus more that I've already forgotten.
Is there a particular technique that's considered the canonically correct or "best practice" method?
Dialogs are normally used for notifications that should interupt the user and to perform short tasks that directly relate to the application in progress (such as a progress bar or a login prompt). The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
There should be no more than three action buttons in a dialog.
Perhaps I'm mis-understanding your question, but why not just use the built in listener system:
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // run whatever code you want to run here // if you need to pass data back, just call a function in your // activity and pass it some parameters } })
This is how I've always handled data from dialog boxes.
EDIT: Let me give you a more concrete example which will better answer your question. I'm going to steal some sample code from this page, which you should read:
http://developer.android.com/guide/topics/ui/dialogs.html
// Alert Dialog code (mostly copied from the Android docs AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick a color"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { myFunction(item); } }); AlertDialog alert = builder.create();
...
// Now elsewhere in your Activity class, you would have this function private void myFunction(int result){ // Now the data has been "returned" (as pointed out, that's not // the right terminology) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With