android: how do I check if dialogfragment is showing

learner picture learner · Jan 25, 2014 · Viewed 52.7k times · Source

I launch my dialog fragment using

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

then to get a handle on it I do

Fragment prev = getFragmentManager().findFragmentByTag("dialog");

but once I get prev, how do I check if it is showing?

Back Story

My problem is that my looping code keeps launching the dialog again and again. But if the dialog is already showing, I don't want it to launch again. This back story is just for context. The answer I seek is not: "move it out of the loop."

Answer

j2emanue picture j2emanue · Jun 25, 2014
 if (dialogFragment != null
     && dialogFragment.getDialog() != null
     && dialogFragment.getDialog().isShowing()
     && !dialogFragment.isRemoving()) {
            //dialog is showing so do something 
 } else {
     //dialog is not showing
 }