I'm trying to make a Fragment
to show a Dialog
using DialogFragment
API.
My dialog only has an EditText
view and I want to pass the string wrote on it back to the Fragment
.
I'm able to show the dialog from the Fragment
, but I'm not able to retrieve the string from the input field back to my Fragment
.
I tried both ways to create a dialog: overwriting the DialogFragment.onCreateView
method and writing the entire dialog's view and overwriting the DialogFragment.onCreateDialog
and using the AlertDialog.Builder
to make it easier to write the buttons behavior.
The Android documentation about dialogs passing events back to dialog's host is about host being always an Activity
and never a Fragment
. I tried to generalize the behavior for use it on my case, but I cannot.
I tried using the DialogFragment.onAttach
callback to get the Activity
reference and use an interface between the Fragment
and the DialogFrament
and it works fine, but I cannot retrieve the Fragment
from the Activity
to make it work.
Can anybody help with a brief example?
Thanks in advance
you can use callbacks. just implement a "done" button or something like that in your dialogfragment.
so in your dialogfragment do something like this:
in your activity do something like this:
You can use the
setTargetFragment
andgetTargetFragment
methods ofFragment
. You can set yourFragment
as the target of theDialogFragment
and then retrieve a reference to it from within theDialogFragment
.