How to check if the current activity has a dialog in front?

后端 未结 8 1880
自闭症患者
自闭症患者 2021-02-05 09:37

I am using a third-party library and sometimes it pops up a dialog. Before I finish the current activity, I want to check whether there is a dialog popped up in the current con

8条回答
  •  爱一瞬间的悲伤
    2021-02-05 10:31

    You can check it running over the active fragments of that activity and checking if one of them is DialogFragment, meaning that there's a active dialog on the screen:

        public static boolean hasOpenedDialogs(FragmentActivity activity) {
            List fragments = activity.getSupportFragmentManager().getFragments();
            if (fragments != null) {
                for (Fragment fragment : fragments) {
                    if (fragment instanceof DialogFragment) {
                        return true;
                    }
                }
            }
    
            return false;
        }
    

提交回复
热议问题