tags:

views:

75

answers:

2

I am using a dialog as a form for the user to input data. When they click the "OK" button, the dialog is closed, and I need to use the data that was entered. How can I reference this data in the activity once the dialog has closed?

A: 

Assign an OnDismissListener to the dialog and pass the data to the activity there.

Alternatively, you can create a dialog activity and return the data as the activity result. See the following link for more info about starting activities and gettings results:

http://developer.android.com/reference/android/app/Activity.html#StartingActivities

hgpc
If you take the OP's question literally ("How can I reference this data in the activity once the dialog has closed?"), then this is a better answer than mine. But I think the OP actually has the ability to access the input *before* the dialog closes--making an OnDismissListener unnecessary.
mxrider
I emphasize the suggestion of using an activity with a dialog theme. Unless it's just one field with no validation, putting form logic in a dialog is a bad idea in the long run.
hgpc
A valid point, especially if the dialog is going to be reused in any other Activities.
mxrider
+1  A: 

Get it when the user presses the "OK" button

final EditText input = new EditText(this); // This could also come from an xml resource, in which case you would use findViewById() to access the input

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString();
        mItem.setValue(value); // mItem is a member variable in your Activity
        dialog.dismiss();
    }
});
mxrider