I am using DialogFragments for a number of things: choosing item from list, entering text.
What is the best way to return a value (i.e. a string or an item from a list) back to the calling activity/fragment?
Currently I am making the calling activity implement DismissListener
and giving the DialogFragment a reference to the activity. The Dialog then calls the OnDimiss
method in the activity and the activity grabs the result from the DialogFragment object. Very messy and it doesn't work on configuration change (orientation change) as the DialogFragment loses the reference to the activity.
Thanks for any help.
In my case I needed to pass arguments to a targetFragment. But I got exception "Fragment already active". So I declared an Interface in my DialogFragment which parentFragment implemented. When parentFragment started a DialogFragment , it set itself as TargetFragment. Then in DialogFragment I called
Just to have it as one of the options (since no one mentioned it yet) - you could use an event bus like Otto. So in the dialog you do:
And have your caller (Activity or Fragment) subscribe to it:
There is a much simpler way to receive a result from a DialogFragment.
First, in your Activity, Fragment, or FragmentActivity you need to add in the following information:
The
requestCode
is basically your int label for the DialogFragment you called, I'll show how this works in a second. The resultCode is the code that you send back from the DialogFragment telling your current waiting Activity, Fragment, or FragmentActivity what happened.The next piece of code to go in is the call to the DialogFragment. An example is here:
With these three lines you are declaring your DialogFragment, setting a requestCode (which will call the onActivityResult(...) once the Dialog is dismissed, and you are then showing the dialog. It's that simple.
Now, in your DialogFragment you need to just add one line directly before the
dismiss()
so that you send a resultCode back to the onActivityResult().That's it. Note, the resultCode is defined as
int resultCode
which I've set toresultCode = 1;
in this case.That's it, you can now send the result of your DialogFragment back to your calling Activity, Fragment, or FragmentActivity.
Also, it looks like this information was posted previously, but there wasn't a sufficient example given so I thought I'd provide more detail.
EDIT 06.24.2016 I apologize for the misleading code above. But you most certainly cannot receive the result back to the activity seeing as the line:
sets a target
Fragment
and notActivity
. So in order to do this you need to use implement anInterfaceCommunicator
.In your
DialogFragment
set a global variableCreate a public function to handle it
Then when you're ready to send the code back to the
Activity
when theDialogFragment
is done running, you simply add the line before youdismiss();
yourDialogFragment
:In your activity now you have to do two things, the first is to remove that one line of code that is no longer applicable:
Then implement the interface and you're all done. You can do that by adding the following line to the
implements
clause at the very top of your class:And then
@Override
the function in the activity,You use this interface method just like you would the
onActivityResult()
method. Except the interface method is forDialogFragments
and the other is forFragments
.Well its too late may be to answer but here is what i did to get results back from the
DialogFragment
. very similar to @brandon's answer. Here i am callingDialogFragment
from a fragment, just place this code where you are calling your dialog.where
categoryDialog
is myDialogFragment
which i want to call and after this in your implementation ofdialogfragment
place this code where you are setting your data in intent. The value ofresultCode
is 1 you can set it or use system Defined.now its time to get back to to the calling fragment and implement this method. check for data validity or result success if you want with
resultCode
andrequestCode
in if condition.Different approach, to allow a Fragment to communicate up to its Activity:
1) Define a public interface in the fragment and create a variable for it
2) Cast the activity to the mCallback variable in the fragment
3) Implement the listener in your activity
4) Override the OnFragmentInteraction in the activity
More info on it: https://developer.android.com/training/basics/fragments/communicating.html
One easy way I found was the following: Implement this is your dialogFragment,
And then in the activity that called the Dialog Fragment create the appropriate function as such:
The Toast is to show that it works. Worked for me.