I am trying to navigate from one screen to another screen using jet pack navigation, but when I back press after navigating, it is keep looping, I seen in log it is recomposing and again navigating to next screen
I am not sure what's wrong but I need help
c
czuckie
09/28/2022, 7:32 AM
When you tap back, your UiState is still going to be UiState.Content which will trigger a navigation by the looks of it.
Navigation is typically triggered by events (button clicks for example) rather than state change. I think this is why the navigation host doesn't take an observable state value as its current destination. You always have to trigger navigation via a navigation controller.
I've not found a brilliant way to manage this, navigation in general always becomes quite tricky in complicated apps and there always seems to be a desire to separate navigation from the view layer, but it's really difficult to do it nicely.
s
Stylianos Gakis
09/28/2022, 12:32 PM
If you’re using state to trigger an event, you need to while triggering the event also make sure to clear that state.
Inspiration can be drawn from this doc, where after reading the state and triggering the event, they’re calling
userMessageShown
which clears the state.
This blogpost is also interesting for this topic.