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
CodePudding user response:
fixed by changing logic inside fragment dialog from:
binding.someButton.setOnClickListener {
setFragmentResult(....)
dismiss()
}
to:
binding.someButton.setOnClickListener {
findNavController().navigateUp() // sync method
setFragmentResult(....)
}