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

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

How to prevent showing multiple DialogFragments in Android?

问题

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

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

  1. fun Fragment.isFragmentVisible(tag: String): Boolean {
  2. val fragment = childFragmentManager.findFragmentByTag(tag)
  3. if (fragment != null && fragment.isVisible) {
  4. return true
  5. }
  6. return false
  7. }

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

  1. binding.destination.editText?.setOnClickListener {
  2. if (!isFragmentVisible(ShopsDialog.TAG)) {
  3. ShopsDialog.newInstance(this, "Destinazione") { bundle ->
  4. val shopId = bundle.getString("shopId")
  5. viewModel.setDestination(shopId)
  6. }
  7. }
  8. }

newInstance函数如下:

  1. fun newInstance(fragment: Fragment, title: String, bundle: (Bundle) -> Unit) = ShopsDialog().apply {
  2. arguments = Bundle().apply {
  3. putString(ARG_TITLE, title)
  4. }
  5. fragment.setFragmentResultListener(KEY_CALLBACK_BUNDLE) { requestKey: String, bundle: Bundle ->
  6. if (requestKey == KEY_CALLBACK_BUNDLE) {
  7. bundle(bundle)
  8. }
  9. }
  10. show(fragment.childFragmentManager, TAG)
  11. }

但是这似乎不起作用,我甚至尝试使用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:

  1. fun Fragment.isFragmentVisible(tag: String): Boolean {
  2. val fragment = childFragmentManager.findFragmentByTag(tag)
  3. if (fragment != null && fragment.isVisible) {
  4. return true
  5. }
  6. return false
  7. }

And before showing the dialog to check it like this:

  1. binding.destination.editText?.setOnClickListener {
  2. if (!isFragmentVisible(ShopsDialog.TAG)) {
  3. ShopsDialog.newInstance(this, "Destinazione") { bundle ->
  4. val shopId = bundle.getString("shopId")
  5. viewModel.setDestination(shopId)
  6. }
  7. }
  8. }

While the newInstance is:

  1. fun newInstance(fragment: Fragment, title: String, bundle: (Bundle) -> Unit) = ShopsDialog().apply {
  2. arguments = Bundle().apply {
  3. putString(ARG_TITLE, title)
  4. }
  5. fragment.setFragmentResultListener(KEY_CALLBACK_BUNDLE) { requestKey: String, bundle: Bundle ->
  6. if (requestKey == KEY_CALLBACK_BUNDLE) {
  7. bundle(bundle)
  8. }
  9. }
  10. show(fragment.childFragmentManager, TAG)
  11. }

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

答案1

得分: 1

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

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

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

英文:

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

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

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:

确定