在 FragmentResultListener 中无法从当前目的地找到导航操作/目的地操作
如果我在 FragmentResultListener
内调用 findNavController()
,导航将不起作用,但如果我添加一些延迟,导航将不起作用
java.lang.IllegalArgumentException:导航操作/目的地 无法从当前目的地找到操作
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
setFragmentResultListener(MyFragment.RESULT_SOME_EVENT) { _, b ->
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
delay(1000L) // if I remove it then I receive the crash
findNavController().navigate(
MyFragmentDirections.actionGoToAnotherFragment()
)
}
}
}
我该如何修复?
我显示一些带有两个按钮的片段对话框,在用户选择某些内容后,它为片段设置结果,对话框关闭,之后在父片段上它应该更改目的地 - 转到下一个片段
我明白为什么会发生这种情况,导航组件认为片段对话框仍然存在显示但我使用 viewLifecycleOwner.lifecycleScope.launchWhenResumed 仍然没有帮助,直到我添加一些延迟
Navigation doesn't work if I call findNavController()
inside FragmentResultListener
, though if I add some delay than it works
java.lang.IllegalArgumentException: Navigation action/destination
action cannot be found from the current destination
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
setFragmentResultListener(MyFragment.RESULT_SOME_EVENT) { _, b ->
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
delay(1000L) // if I remove it then I receive the crash
findNavController().navigate(
MyFragmentDirections.actionGoToAnotherFragment()
)
}
}
}
How can I fix it?
I show some fragment dialog with two buttons, after user selects something it set result for fragment, dialog dismiss and after that on parent fragment it should change destination - go to next fragment
I understand why it happens, navigation component thinks that fragment dialog is still showing but I use viewLifecycleOwner.lifecycleScope.launchWhenResumed
and still it doesn't help, only until I add some delay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将片段对话框内的逻辑从: 更改
为: 来修复
fixed by changing logic inside fragment dialog from:
to: