我有一台DialogFragment
。NavController库不支持DialogFragment
,它只支持全屏片段。
我正在使用DialogFragment
和show(fragmentManager,"")
,它工作得很好,但在DialogFragment
中我无法获得“导航”实例。(可能是因为它不是以NavController开头的)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)
dialogView = LayoutInflater.from(context).inflate(R.layout.fragment_dialog, null)
viewModel = BaseViewModel.create(this)
return AlertDialog.Builder(context)
.setView(dialogView)
.create()
}
我试过了(在onCreateDialog,onActivityCreated中)
Navigation.findNavController(view)
Navigation.findNavController(customView)
Navigation.findNavController(activity, R.id.thisFragmentId)
有没有使用NavController
加载新片段的选项?如果不是,有没有其他的解决方案?我喜欢它,因为我可以在DialogFragment
中使用ViewModel
。
发布于 2018-12-07 16:51:10
我找到了答案。但我在寻找一个更好的解决方案。
val args = DirectionFragmentArgs.Builder()
.setId(id)
.build()
.toBundle()
(activity as SingleActivity).navigation.navigate(R.id.directionFragmentId, args)
dismiss()
https://stackoverflow.com/questions/53665861
复制相似问题