使用bottomsheet的操作打开另一个dialogfragment,而不会崩溃android导航组件

问题描述 投票:0回答:1

我有片段A,BottomSheetDialogFragment B和DialogFragmentC。我希望能够从B中选择一个动作,解散B返回A,然后使用导航实时数据从A导航到C。

A和B共享相同的activityViewModel。我的意图是,当单击B中的操作按钮时,它将关闭B,在activityViewModel中修改一个标志,并且A将观察到对该标志的更改并随后打开C。但是,我得到一个错误,并且我认为它认为我正在从B导航到C(而不是从A到C):

java.lang.IllegalArgumentException:导航目标... action_FragmentA_to_DialogFragmentC对于此NavController未知

我相信例外是由于程序认为动作是从B到C。

class DetailBottomSheet : BottomSheetDialogFragment() {

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    private val detailViewModel by activityViewModels<DetailViewModel>() { viewModelFactory }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding.bottomSheetAction.setOnClickListener {
            dismiss()
            detailViewModel.setBottomSheetDialogType(1)
        }
    }
}

class DetailFragment : Fragment() {

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    private val detailViewModel by activityViewModels<DetailViewModel>() { viewModelFactory }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        detailViewModel.bottomSheetDialogType.observe(viewLifecycleOwner, Observer {
            when (it) {
                1 -> detailViewModel.navigateToDialogFragmentC(args.id)
                else -> {}
            }
        })

        detailViewModel.navigateToDialogFragmentC.observe(
            viewLifecycleOwner,
            WrapperEventObserver {
                Thread.sleep(500) // thought this would allow wait time for bottomsheet to dismiss, but doesn't make a difference
                val action =
                    DetailFragmentDirections.actionFragmentAToDialogFragmentC(it)
                findNavController().navigate(action)
            })
    }
}

在这种情况下,我可以使用哪些修复程序正常浏览?

android android-dialogfragment android-architecture-components bottom-sheet android-architecture-navigation
1个回答
1
投票

在任何dismiss()上调用DialogFragment都会异步更新NavController状态-这意味着从NavController的角度来看,当DetailBottomSheet观察员被解雇时,您are

© www.soinside.com 2019 - 2024. All rights reserved.