https://kotlinlang.org logo
#compose
Title
# compose
t

Tim Malseed

02/07/2022, 1:25 AM
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

Ian Lake

02/07/2022, 1:39 AM
Is it important that the entire hierarchy is part of the route? Is
topic/{topicId}
different semantically from
topic/{subTopicId}
?
t

Tim Malseed

02/07/2022, 1:41 AM
There’s no difference, but the hierarchy is structured.. So you should be able to navigate back through previous destinations (ideally)
i

Ian Lake

02/07/2022, 1:42 AM
The structure of the route string has no impact on whether you can go back to previous destinations
t

Tim Malseed

02/07/2022, 1:43 AM
Oh, I assumed the route and its arguments might be used to rebuild the stack after process death
i

Ian Lake

02/07/2022, 1:44 AM
The NavController is responsible for maintaining the back stack; it already saves and restores that stack for you
t

Tim Malseed

02/07/2022, 1:45 AM
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

Ian Lake

02/07/2022, 1:46 AM
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

Tim Malseed

02/07/2022, 1:48 AM
Oh, that definitely could be my problem!