android: how do I check if dialogfragment is showing

◇◆丶佛笑我妖孽 提交于 2019-11-29 22:47:35

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"
j2emanue
 if (dialogFragment != null
     && dialogFragment.getDialog() != null
     && dialogFragment.getDialog().isShowing()
     && !dialogFragment.isRemoving()) {
            //dialog is showing so do something 
 } else {
     //dialog is not showing
 }
John Leehey

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!