I am using a DialogFragment
, which I am showing like this from an Activity
:
DialogFragmentImage dialog = DialogFragmentImage.newInstance(createBitmap()); dialog.onDismiss(dialog);.onDismiss(this); dialog.show(getFragmentManager(), "DialogFragmentImage");
I would like to check when the DialogFragment
was dismissed (for example when the back button was pressed), but in my Activity
. How can I do that? How can I "tell" my activity
that the DialogFragment
has been dismissed?
DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.
that should mean its in the foreground displaying if im not mistaken. If you call DialogFragment's creation several times in one moment, dialogFragment = getSupportFragmentManager(). findFragmentByTag("dialog"); will return null, and all dialogs will be shown.
Make your Activity implement OnDismissListener
public final class YourActivity extends Activity implements DialogInterface.OnDismissListener { @Override public void onDismiss(final DialogInterface dialog) { //Fragment dialog had been dismissed } }
DialogFragment already implements OnDismissListener
, just override the method and call the Activity.
public final class DialogFragmentImage extends DialogFragment { ///blah blah @Override public void onDismiss(final DialogInterface dialog) { super.onDismiss(dialog); final Activity activity = getActivity(); if (activity instanceof DialogInterface.OnDismissListener) { ((DialogInterface.OnDismissListener) activity).onDismiss(dialog); } } }
If you're starting the dialog from a fragment using the childFragment
manager (API>=17), you can use getParentFragment
to talk to the onDismissListener on the parent fragment.:
public final class DialogFragmentImage extends DialogFragment { ///blah blah @Override public void onDismiss(final DialogInterface dialog) { super.onDismiss(dialog); Fragment parentFragment = getParentFragment(); if (parentFragment instanceof DialogInterface.OnDismissListener) { ((DialogInterface.OnDismissListener) parentFragment).onDismiss(dialog); } } }
Here is my answer. It's a bit late but it's maybe benefit someone passing by.
FragmentManager fm = getFragmentManager(); YourDialogFragment dialog = new YourDialogFragment(); dialog.show(fm,"MyDialog"); fm.executePendingTransactions(); dialog.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialogInterface) { //do whatever you want when dialog is dismissed } });
We need to call
fm.executePendingTransactions();
To make sure that FragmentTransaction work has been performed. Otherwise NullPointerException
can occur when calling setOnDismissListener()
.
Sorry if there is any mistake. Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With