Is there a way to implement a sort of recursive na...
# compose
t
Is there a way to implement a sort of recursive navigation route with compose navigation? In this app, you can navigate to, e.g.
topics/topic1
, or
topics/topic1/topics/topic2
. The depth of recursion is undefined.. I’m not sure how to implement this, as implementing a composable for each possible level, e.g.
composable(topic/{topicId}/subTopic{subTopicId}/moreSubtopic/{moreSubTopicId}
doesn’t seem practical
i
Is it important that the entire hierarchy is part of the route? Is
topic/{topicId}
different semantically from
topic/{subTopicId}
?
t
There’s no difference, but the hierarchy is structured.. So you should be able to navigate back through previous destinations (ideally)
i
The structure of the route string has no impact on whether you can go back to previous destinations
t
Oh, I assumed the route and its arguments might be used to rebuild the stack after process death
i
The NavController is responsible for maintaining the back stack; it already saves and restores that stack for you
t
My initial attempt naively just used the route
myscreen/topics/{topicId}
. Each time you want to navigate a level deeper, it calls
navController.navigate("myscreen/topics/${topic.id}
But, when I attempt to retrieve the
topicId
from the
NavBackStackEntry
, once I’m more than one level deep, I seem to get a stale one. Which is what made me think this approach was incorrect.
i
As long as you are getting the argument out of the
NavBackStackEntry
that is explicitly passed to your
composable
lambda, you'll get the exact set of arguments you passed into it
(you would not want to use
navController.currentBackStackEntry
, etc.)
Note that both destinations will exist simultaneously while you are animating to the new destination, so make sure it isn't just your logging that is confusing you
t
Oh, that definitely could be my problem!