android:如何检查dialogfragment是否显示

时间:2014-01-25 15:40:20

标签: android android-layout android-fragments android-dialogfragment

我使用

启动对话框片段
FragmentTransaction ft = 
getFragmentManager().beginTransaction();
MyDialogFragment dialog = new MyDialogFragment()
dialog.show(ft, "dialog");

然后我要处理它

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

但是一旦我得到prev,我该如何检查它是否显示?

返回故事

我的问题是我的循环代码不断反复启动对话框。但是如果对话框已经显示,我不希望它再次启动。 这个背景故事仅供上下文使用。我寻求的答案不是:“将它移出循环。”

5 个答案:

答案 0 :(得分:88)

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

答案 1 :(得分:47)

我将其添加到我的自定义对话框片段中,因此我不必担心外部的任何逻辑。使用show()字段覆盖onDismiss()boolean shown方法:

  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);
    }

如果要检查是否显示,可以为shown布尔值创建一个getter。

答案 2 :(得分:22)

只需检查它是否为空

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.

或者,您可以检查片段prev当前与之关联的活动,但是,请确保在 之后确认它不是null或者您将获得NullPointerException 。像这样:

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"

答案 3 :(得分:0)

100% 正常运行时间


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

初始化:?


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
    }
}

到处使用{ progressDialog.showDialog() } ?


附注。 Vel_daN:爱​​你所做的?。

答案 4 :(得分:-1)

科特琳风格:

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