android: how do I check if dialogfragment is showing

后端 未结 4 1604
臣服心动
臣服心动 2020-12-23 19:15

I launch my dialog fragment using

FragmentTransaction ft = 
getFragmentManager().beginTransaction();
MyDialogFragment dialog = new MyDialogFragment()
dialog.         


        
4条回答
  •  孤城傲影
    2020-12-23 19:21

    I added this to be inside my custom dialog fragment, so I don't have to worry about any logic on the outside. Override the show() and onDismiss() methods, with a boolean shown field:

      private static boolean shown = false;
    
        @Override
        public void show(FragmentManager manager, String tag) {
            if (shown) return;
    
            super.show(manager, tag);
            shown = true;
        }
    
        @Override
        public void onDismiss(DialogInterface dialog) {
            shown = false;
            super.onDismiss(dialog);
        }
    

    If you want to check whether it is shown or not, you can create a getter for the shown boolean.

提交回复
热议问题