Dismiss all DialogFragments added to given FragmentManager and child fragments - Android User Interface

Android examples for User Interface:Dialog

Description

Dismiss all DialogFragments added to given FragmentManager and child fragments

Demo Code


//package com.java2s;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import java.util.List;

public class Main {
    /**//from  w  ww . j  ava2s .co m
     * 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 == null)
                continue;

            if (fragment instanceof DialogFragment) {
                DialogFragment dialogFragment = (DialogFragment) fragment;
                dialogFragment.dismissAllowingStateLoss();
            }

            FragmentManager childFragmentManager = fragment
                    .getChildFragmentManager();
            if (childFragmentManager != null)
                dismissAllDialogs(childFragmentManager);
        }
    }
}

Related Tutorials