如何防止在Android中显示多个DialogFragments?

huangapple go评论61阅读模式
英文:

How to prevent showing multiple DialogFragments in Android?

问题

我在我的Android应用程序中有一些自定义的DialogFragments。我想要防止用户打开多个对话框,同时避免禁用显示对话框的按钮一次按下后或使用变量。

所以我尝试为片段制作一个扩展,通过它们的标签来检查显示的片段,就像这样:

fun Fragment.isFragmentVisible(tag: String): Boolean {
    val fragment = childFragmentManager.findFragmentByTag(tag)
    if (fragment != null && fragment.isVisible) {
        return true
    }
    return false
}

在显示对话框之前进行检查,就像这样:

binding.destination.editText?.setOnClickListener {
    if (!isFragmentVisible(ShopsDialog.TAG)) {
        ShopsDialog.newInstance(this, "Destinazione") { bundle ->
            val shopId = bundle.getString("shopId")
            viewModel.setDestination(shopId)
        }
    }
}

newInstance函数如下:

fun newInstance(fragment: Fragment, title: String, bundle: (Bundle) -> Unit) = ShopsDialog().apply {
    arguments = Bundle().apply {
        putString(ARG_TITLE, title)
    }

    fragment.setFragmentResultListener(KEY_CALLBACK_BUNDLE) { requestKey: String, bundle: Bundle ->
        if (requestKey == KEY_CALLBACK_BUNDLE) {
            bundle(bundle)
        }
    }
    show(fragment.childFragmentManager, TAG)
}

但是这似乎不起作用,我甚至尝试使用requireActivity().supportFragmentManager而不是childFragmentManager,但仍然没有效果...

英文:

I have some custom DialogFragments in my Android application. I would prevent the user from opening multiple dialogs, and I would also avoid disabling the button which shows the dialog once it's pressed or using a variable or it.

So I was trying to make an extension for the fragment which checks for shown fragments via their tags like this:

fun Fragment.isFragmentVisible(tag: String): Boolean {
    val fragment = childFragmentManager.findFragmentByTag(tag)
    if (fragment != null && fragment.isVisible) {
        return true
    }
    return false
}

And before showing the dialog to check it like this:

    binding.destination.editText?.setOnClickListener {
        if (!isFragmentVisible(ShopsDialog.TAG)) {
            ShopsDialog.newInstance(this, "Destinazione") { bundle ->
                val shopId = bundle.getString("shopId")
                viewModel.setDestination(shopId)
            }
        }
    }

While the newInstance is:

   fun newInstance(fragment: Fragment, title: String, bundle: (Bundle) -> Unit) = ShopsDialog().apply {
        arguments = Bundle().apply {
            putString(ARG_TITLE, title)
        }

        fragment.setFragmentResultListener(KEY_CALLBACK_BUNDLE) { requestKey: String, bundle: Bundle ->
            if (requestKey == KEY_CALLBACK_BUNDLE) {
                bundle(bundle)
            }
        }
        show(fragment.childFragmentManager, TAG)
    }

But this seems not to work, I've tried even to use requireActivity().supportFragmentManager instead childFragmentManager but still nothing...

答案1

得分: 1

在显示底部对话框片段之前,我认为你可以像这样检查它

if (childFragmentManager.findFragmentByTag(ShopsDialog::class.java.simpleName) == null) {
   // 显示你的底部对话框片段
}

如果你要在 Fragment 中显示 BottomSheetDialogFragment,那么请使用 childFragmentManager,如果在 Activity 中显示,则使用 supportFragmentManager

英文:

Before showing bottomsheetdialogfragment , i think you can check it like this

if(childFragmentManager.findFragmentByTag(ShopsDialog::class.java.simpleName) == null){
   // show your bottom sheet dialog fragment
  
}

If you are going to show bottomSheetDialogFragment in Fragment then use childFragmentManager and if it is activity then use supportFragmentManager

huangapple
  • 本文由 发表于 2023年2月8日 19:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定