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 abl
Solution 1:
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
.
Solution 2:
you can use callbacks. just implement a "done" button or something like that in your dialogfragment.
so in your dialogfragment do something like this:
protectedOnDialogClickedListenercallback=null;
publicinterfaceOnDialogClickedListener {
publicabstractvoidonDialogClicked(int position);
}
publicvoidsetOnDialogClickedListener(OnDialogClickedListener l){
callback = l;
}
button.setOnItemClickListener(newOnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View v, int position, long id) {
callback.onDialogClicked(position);
dismiss();
}
});
in your activity do something like this:
finalYourFragmentf= YourFragment .newInstance(0);
f.show(ft, "YourFragment ");
f.setOnDialogClickedListener(newOnDialogClickedListener() {
@OverridepublicvoidonDialogClicked(int position) {
updateText(position);
f.dismiss();
}
});
Post a Comment for "Communication Between Fragment And Dialogfragment"