android-fragments android:如何检查是否显示了对话片段

oewdyzsn  于 2022-11-14  发布在  Android
关注(0)|答案(5)|浏览(152)

我使用以下命令启动对话框片段

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

然后我就去处理了

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

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

背景故事

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

xoefb8l8

xoefb8l81#

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

更新:团队也可以尝试下面的调用,我认为它将工作逻辑:

dialogFragment.isResumed

这应该意味着它在前景显示,如果我没有错的话。

9jyewag0

9jyewag02#

我把它添加到我的自定义对话框片段中,这样我就不必担心外部的任何逻辑。用一个boolean shown字段覆盖show()onDismiss()方法:

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。

jdzmm42g

jdzmm42g3#

只需检查它是否为空

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当前关联的Activity,但是,请确保您在之后询问,确保它不为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"
wvmv3b1j

wvmv3b1j4#

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() }😎

e5njpo68

e5njpo685#

Kotlin风格:

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

相关问题