Getting Data from Android Dialogs
This is yet another installment in a series of blogs that should be called "Another Way Android is Not Like Swing." Although that's not the subtitle...perhaps it should be.
In Swing, I often create dialogs that allow me to access data after they are closed (this includes checking to see if okay or cancel was pressed). This is certainly not a stellar example of design, but not all applications require complete MVC implementations. Right or wrong, Android is not having anyof it. No dice. Find another way.
What about the examples?
Yes, the online Android documentation has very kindly provided two examples: a date picker dialog and a time picker dialog. Both use a pre-defined event listener and then set the data in local class variables. Although I may not design everything exactly right, polluting my class with extra variables is going too far.
A Swing and a Miss
This blogger recommends creating a listener interface and then implementing that interface. This is a pretty simple solution, but I don't like the idea of creating an interface just to accomodate a callback. Furthermore, this creates a circular coupling that really bothers me. If I am going to couple code, I would rather have the violation contained.
Another blogger recommends extending Activity and then launching the dialog as an Activity that returns a Bundle. This sounded like a great idea with a slightly better architecture than the former, so I started with this.
In order to launch the dialog activity, I needed to issue the following command:
- Intent dialog = new Intent(this, MyCustomDialog.class);
- startActivityForResult(dialog, MyCustomDialog.ID);
This might have worked...but my custom dialog class needed to be instantiated - not merely referenced as a class object. Bummer. At the very least, this approach did suggest the use of Bundles to return data. This got me thinking about the Interface solution that I didn't like. Would it be possible to develop an interface that would not be constrained to this single dialog?
A Simple, Flexible Solution
So I did just that. I created the following interface:
- package com.chris_allport.utils;
-
- import android.os.Bundle;
-
- public interface DialogReturnListener {
- void reportDialogData(Bundle bundle);
- }
-
By using the Bundle, any dialog can return any set of data and the application can use the standard showDialog mechanism to launch the dialog.
This does introduce one potential problem. If the initiating activity implements the listener, how does it discriminate which bundle it receives? I was hoping you would ask that!
Originally, I was going to include a reference identifier in the method (just like in startActivityForResults). Instead, I opted for inlining the callback like so:
- dialog = new MyCustomDialog(this, new DialogReturnListener() {
- public void reportDialogData(Bundle bundle) {
- // Your Code Goes Here
- }
- });
-
What's the verdict? You decide. Right now, it appears to be working just fine. Sure, the Bundle is overkill to return a single string (my current application), but at this point, it's worth the flexibility.

