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.
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 toYourActivity.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.