Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to Dismiss All Dialogs in onPause

Tags:

android

dialog

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?

like image 587
AndyB Avatar asked Feb 08 '12 21:02

AndyB


People also ask

How do I dismiss all dialogs in Android?

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.

How do you dismiss a DialogFragment?

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.

What is difference between dialog and DialogFragment in Android?

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.


1 Answers

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);     } } 
like image 132
Dominik Suszczewicz Avatar answered Sep 20 '22 11:09

Dominik Suszczewicz