android: how do I check if dialogfragment is showing

后端 未结 4 1602
臣服心动
臣服心动 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:16

    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"
    

提交回复
热议问题