Hey Team! I’m curious about some code I’ve seen to...
# android
j
Hey Team! I’m curious about some code I’ve seen tons of times:
Copy code
viewModel.navigate.collect {
    it?.let {
        viewModel.onNavigated()
        findNavController().popBackStack(R.id.home, false)
        findNavController().navigate(it)
    }
}
It works great, but I’m curious about why the navigation is executed if, once you execute the popBackStack, the current fragment should no longer exist. Is it a race condition? Or is it intentional that before killing the fragment, the current block of code is completed? Also, this is in a launch block, so when the onDestroy of the fragment is executed, the job gets cancelled. If you need more info, putting a delay between one line and another (even just 1 ms) causes it to stop working, meaning it destroys the fragment. So I think that is something that happens on purpose.
p
It would probably be better to use
popUpTo
j
Yes! But in this particular case I’m navigating through global actions to dialogs that are all over the app, so i can’t do this.
I also just want to know what is going on hehe
p
Not sure why it would behave differently, calling
navigate
with
popUpTo
calls
popBackStack
internally 🤔
j
ok, but do you understand the point i’m making?
p
Not sure why it works tbh 😄 I would guess that the job hasn't been cancelled before the fragment is destroyed
j
but, is it on purpose? or just luckl?