harry248
07/05/2023, 6:59 AMMR3Y
07/05/2023, 11:25 AMnavController.currentBackStackEntryAsState()
State, and then search for the base route(the one without arguments) in the current destination's hierarchy, and then navigate to that base route.
Example code snippet:
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
val baseDestination = currentDestination?.hierarchy?.first { it.arguments.isEmpty() }
// ...
navController.navigate(baseDestination?.route) {
popUpTo(baseDestination?.id) {
// ...
}
}
harry248
07/05/2023, 11:57 AMhierarchy
property. Looks good, will try, thanks!harry248
07/05/2023, 12:19 PMStylianos Gakis
07/05/2023, 12:30 PMhierarchy
may not be what you want here though right?
The hierarchy will just give you the chain of destinations that are above the current destination.
That will be the root destination of your NavHost
Then if you are inside a sub nav graph, it will be the route of that graph.
And then all other nav graph routes that are above you still, if that destination is quite nested.
If you want to find the ID of the top-most destination of that specific route, I think you’d need to check the the stack of the entries instead, and not only the hierarchy of the current one. So like inside navController.currentBackStack
, that returns a List<NavBackStackEntry>
and look through all of those, check if their destination.route is what you need and then get that ID to know where to pop up to?
Something like
navController.currentBackStack.value.firstOrNull { navBackStackEntry ->
navBackStackEntry.destination.route == "yourRoute"
}?.id
public val NavDestination.hierarchy
as the docs describe:
“”"
Provides a sequence of the NavDestination’s hierarchy. The hierarchy starts with this
destination itself and is then followed by this destination’s [NavDestination.parent], then that
graph’s parent, and up the hierarchy until you’ve reached the root navigation graph.
“”"
So it doesn’t look at your current backstack, and I think this is what you’re after in this scenario.harry248
07/05/2023, 12:54 PMcurrentBackStack
but as I've mentioned, this api is restricted and who knows when it will be private (like backQueue).Stylianos Gakis
07/05/2023, 1:02 PM@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
annotation.
The getBackStackEntry
function is so close to what you need here https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]x/navigation/NavController.kt;l=2414-2423?q=getBackStackEntry
But it by itself does a lastOrNull
call on the private backQueue
.
If there was another API exposed which let you receive all those options and pick the one that you need then that would already be enough (in your case it’d be firstOrNull
instead).
I wonder why this is not exposed to the public. Maybe time for a feature request? I’d definitely +1 that to see what the library owners think about exposing such an alternative API there.Stylianos Gakis
07/05/2023, 1:05 PMpopUpTo
function here https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]4-104?q=popUpTo&ss=androidx%2Fplatform%2Fframeworks%2Fsupport takes in an ID but it’s a @IdRes
so maybe this is only meant to work for the situation where your destinations are defined in XML, and not in pure compose?
Maybe there isn’t a way to pop to a specific destination which you can identify through an ID instead of the route in the compose version of navigation? Could be totally wrong here, but this @IdRes annotation does not give me a lot of confidence 😄harry248
07/05/2023, 1:07 PMIan Lake
07/05/2023, 1:25 PMIan Lake
07/05/2023, 1:26 PMharry248
07/05/2023, 1:28 PMIan Lake
07/05/2023, 1:32 PMIan Lake
07/05/2023, 1:33 PMharry248
07/06/2023, 5:01 AMIan Lake
07/06/2023, 5:42 AMIan Lake
07/06/2023, 5:44 AMIan Lake
07/06/2023, 5:45 AMIan Lake
07/06/2023, 5:49 AMIan Lake
07/06/2023, 5:52 AMharry248
07/06/2023, 6:39 AMStylianos Gakis
07/06/2023, 7:04 AMharry248
07/06/2023, 11:51 AMIan Lake
07/06/2023, 1:48 PMIan Lake
07/06/2023, 1:51 PMIan Lake
07/06/2023, 1:52 PMharry248
07/07/2023, 5:14 AMcurrentBackStack
. Our ViewModels use UseCases from the KMM part and with the proposed solution we would have to emit in a flow which UseCase has to load new data. The UseCases aggregate data from one or more repositories. The repositories do not know which UseCases are using them, so we cannot do the emission there. It is also not possible in the UseCases, because the repositories are used by many UseCases, and a UseCase does not know which other UseCases are using the same repository. This leaves the ViewModel, but the ViewModel does not know which UseCases have been used by the ViewModels in the back stack. Of course, a solution could be found, but simply "invalidating" the backstack seemed simple and effective.