android: how do I check if dialogfragment is showing

AndroidAndroid LayoutAndroid FragmentsAndroid Dialogfragment

Android Problem Overview


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."

Android Solutions


Solution 1 - Android

 if (dialogFragment != null
     && dialogFragment.getDialog() != null
     && dialogFragment.getDialog().isShowing()
     && !dialogFragment.isRemoving()) {
            //dialog is showing so do something 
 } else {
     //dialog is not showing
 }

UPDATE: team can you also try the below call, i think it will work logically:

dialogFragment.isResumed

that should mean its in the foreground displaying if im not mistaken.

Solution 2 - Android

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.

Solution 3 - Android

simply check if it's null

if(prev == null)
    //There is no active fragment with tag "dialog"
else
    //There is an active fragment with tag "dialog" and "prev" variable holds a reference to it.

Alternatively, you could check the activity the fragment prev is currently associated with, however, make sure you ask that after you make sure it's not null or you'll get a NullPointerException. Like this:

if(prev == null)
    //There is no active fragment with tag "dialog"
else
    if(prev.getActivity() != this) //additional check
        //There is a fragment with tag "dialog", but it is not active (shown) which means it was found on device's back stack.
    else
        //There is an active fragment with tag "dialog"

Solution 4 - Android

100% Uptime

class ProgressDialogFragment : DialogFragment() {

 companion object {
    private val TAG = this::class.simpleName
 }

 lateinit var dialogFragmentManager: FragmentManager
 
 fun showDialog() {
    dialogFragmentManager.apply {
       if (findFragmentByTag(TAG) == null) show(this, TAG) else return
    }
 }

}

lateinit var progressDialog: ProgressDialogFragment

Init: 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
   
    initProgressDialog(supportFragmentManager)
}

fun initProgressDialog(fragmentManager: FragmentManager){
    progressDialog = ProgressDialogFragment().apply {
        dialogFragmentManager = fragmentManager
    }
}

Use { progressDialog.showDialog() } everywhere 

Solution 5 - Android

Kotlin style:

private fun showDialog(dialogFragment: DialogFragment, tag: String) {
    supportFragmentManager.findFragmentByTag(tag).let { fragment ->
        fragment ?: let {
            supportFragmentManager.beginTransaction().let { transition ->
                dialogFragment.show(transition, tag)
            }
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlearnerView Question on Stackoverflow
Solution 1 - Androidj2emanueView Answer on Stackoverflow
Solution 2 - AndroidJohn LeeheyView Answer on Stackoverflow
Solution 3 - AndroidnstosicView Answer on Stackoverflow
Solution 4 - AndroidВладислав ШестернинView Answer on Stackoverflow
Solution 5 - AndroiddrindtView Answer on Stackoverflow