Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between Fragment and DialogFragment

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

like image 390
JoseLSegura Avatar asked Dec 24 '12 23:12

JoseLSegura


People also ask

How can fragment and activity communicate with each other?

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods to communicate with the Activity.

What is the difference between dialog & DialogFragment?

DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. It allows the FragmentManager to manage the state of the dialog and automatically restore the dialog when a configuration change occurs.


2 Answers

You can use the setTargetFragment and getTargetFragment methods of Fragment. You can set your Fragment as the target of the DialogFragment and then retrieve a reference to it from within the DialogFragment.

like image 181
patheticpat Avatar answered Nov 10 '22 02:11

patheticpat


you can use callbacks. just implement a "done" button or something like that in your dialogfragment.

so in your dialogfragment do something like this:

protected OnDialogClickedListener callback = null;

public interface OnDialogClickedListener {
    public abstract void onDialogClicked(int position);
}

public void setOnDialogClickedListener(OnDialogClickedListener l){
    callback = l;
}

button.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        callback.onDialogClicked(position);
        dismiss();
    }
});

in your activity do something like this:

final YourFragment f = YourFragment .newInstance(0);
f.show(ft, "YourFragment ");
f.setOnDialogClickedListener(new OnDialogClickedListener() {
    @Override
    public void onDialogClicked(int position) {
        updateText(position);
        f.dismiss();
    }
});
like image 44
ha1ogen Avatar answered Nov 10 '22 03:11

ha1ogen