Android – How to Dismiss All Dialogs in onPause

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?

  • Build Android native library with Adobe Air
  • dpi value of default “large”, “medium” and “small” text views android
  • Changing API level Android Studio
  • store long value in android database
  • Checking a checkbox in listview makes other random checkboxes checked too
  • Duplicate files during packaging of an APK
  • How to add a system service to the Android Framework
  • Hide Soft Keyboard
  • Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment variable to
  • How can I customize the code that is generated when I use “Surround with try/catch”?
  • Android TransitionDrawable with multiple items
  • Apply ARGB color to a textview programmatically
  • 3 Solutions collect form web for “Android – How to Dismiss All Dialogs in onPause”

    Depending on how many dialog’s we’re talking about. The short answer is yes, you’ll have to dismiss each dialog.

    There may be elegant ways of doing this other than simply having a few dialogs declared at the activity level. You could store all the dialogs in a HashMap once they are declared and then iterate through the list and close each one onPause.

    Since you don’t know which are open you’ll need to go through and test or track the states.

    However, if you truly have this many dialogs on your screen you may have some issues with your UI/UX design as Android should give you a model which makes it easy to implement it without what seems like poor design.

    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);
        }
    }
    

    With android’s recent emphasis on using DialogFragment container you wil not need to call dismiss on each

    Since the dialogs will have a Fragment container you may simply use their lifecycle. Consider this DialogFragment:

    public class FragDialog extends DialogFragment{
    
        public ProgressDialog _dialog;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            if (savedInstanceState != null) {
                //this.dismiss();     <-- The dialogs may be dismissed here
            }
        }
    
        @Override
        public Dialog onCreateDialog(final Bundle savedInstanceState) {
    
            _dialog = new ProgressDialog(getActivity());
            return _dialog;
        }
    
        @Override
        public void onPause() {
            super.onPause();
            // <--------- You may overload onPause
        }
    }
    

    Which you will show in your activity using a fragmentmanager normally calling it like this:

    FragmentManager fm = getSupportFragmentManager();
    FragDialog fragDialog = new FragDialog();
    fragDialog.show(fm, "my_tag");
    

    Note that you may actually alter what the DialogFragment does in onPause. When your activity calls onPause, this onPause will be called too.

    Dismissing the dialog in onPause() using this.dismiss() won’t do the work because once the activity resumes it will resume the dialog as well. (I think this is because the savestate is stored prior to onPause).

    But you can safely dismiss the dialog(s) in onCreate if you detect a savedInstanceState (a resume) like shown in the code.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.