2

I am trying to create a custom dialog class that inherits from Dialog that will allow the user to choose a contact. I can get the contact choose to show up just fine by using:

this.getOwnerActivity().startActivityForResult(...);

The problem is that this calls the onActivityResult() on the dialog's parent activity when the user chooses a contact; I want to encapsulate the handling of the activity result in the dialog class, not the parent activity.

This chap asked the same question, but did not receive a viable answer:

inside Android Dialog, how to setup onActivityResult for startActivityForResult?

Is this possible? Seems like a pretty basic thing to want to do, so I would think there is way.

I could convert the dialog to a whole new activity, but the dialog works very nicely within the context of the app so I would rather not have to resort to that.

Community
  • 1
  • 1
Mekon
  • 101
  • 1
  • 9

2 Answers2

5

The dialog is secondary to its activity. When you start the activity with startActivityForResult(), your dialog gets dismissed (and your activity may get recycled). So when you return to YourActivity.onActivityResult(), the dialog is not active, in fact the original dialog object doesn't exist any more. You can instantiate the dialog again, show it, pass the necessary data to it, and have it do something, but that seems like a very bad design. Instead, process your results in the activity and open a dialog to communicate with the user.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • I understand your point, BUT I have this entire process working just fine IF the parent activity handles building the dialog and handling the activity result; the user interaction is exactly the same: The dialog is up, the user chooses "Choose contact", the contact chooser is shown, the user – Mekon Mar 09 '12 at 01:55
  • 1
    Sorry, that was weird, losing part of my comment. I have this entire process working just fine IF the parent activity handles building the dialog, starts the contact chooser via the startActivityForResult() and handles the activity result; the user interaction is exactly the same as what I am asking about above: The dialog is shown, the user chooses "Choose contact", the contact chooser is shown, the user chooses a contact, and the dialog is updated with the new contact. I simply want to encapsulate this in a custom dialog class rather than having the parent activity handle everything. – Mekon Mar 09 '12 at 16:45
  • I see. I stand by my answer though: when you open the contact chooser, your dialog is dismissed and GC'd. Upon returning to the first activity, it needs to display the same dialog with the new contact details. I just don't think there's any other way. – TotoroTotoro Mar 09 '12 at 18:00
  • Okay, not sure about the particulars, but I am convinced at this point that encapsulating this in a Dialog class is not possible (which is unfortunate), so I went ahead and accepted your answer. – Mekon Mar 09 '12 at 21:12
  • If the only way is to re-instantiate the dialog, is it really "bad design"? I'm doing something similar to this but instead of choosing contacts I am allowing the user to choose media files. I also have some more "settings" in the dialog which the user also need to set. Would it be better to use an Activity instead of Dialog? The Dialog seems like a better UX choice to me even if the class-design gets messy :/ – span Sep 27 '12 at 13:28
1

You can use DialogFragment instead of Dialog. @BlackRider already answered The dialog is secondary to its activity. When you start the activity with startActivityForResult(), your dialog gets dismissed

Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41