I have an activity that could show different dialogs during run-time. I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.
When onPause() is called, I don't know which dialog (if any) is being displayed. I want to make sure that when onPause is called, all dialogs are dimissed. Is there a recommended way to dismiss all dialogs? Would I have to call dismissDialog() for each dialog?
I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.
tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.
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.
If you are using DialogFragment and you want to dismiss all you can use:
/** * Dismiss all DialogFragments added to given FragmentManager and child fragments */ public static void dismissAllDialogs(FragmentManager manager) { List<Fragment> fragments = manager.getFragments(); if (fragments == null) return; for (Fragment fragment : fragments) { if (fragment instanceof DialogFragment) { DialogFragment dialogFragment = (DialogFragment) fragment; dialogFragment.dismissAllowingStateLoss(); } FragmentManager childFragmentManager = fragment.getChildFragmentManager(); if (childFragmentManager != null) dismissAllDialogs(childFragmentManager); } }
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