Hi! How do I check what the current destination (o...
# compose-android
d
Hi! How do I check what the current destination (object/class) is with the new type-safe navigation in 2.8.0?
m
if you define screens in a sealed class like I do I use this method to check the current destination
Copy code
val backStackEntry by navController.currentBackStackEntryAsState()
if(backStackEntry?.destination?.route == Screen.Home::class.qualifiedName){
    // do something
}
👎 1
d
Thanks! So the
route
is the qualifiedName...
k
Similar option is something like:
Copy code
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination

when {
    destination.hasRoute(FooRoute::class) -> TODO()
    destination.hasRoute(BarRoute::class) -> TODO()
    else -> TODO()
}
m
Copy code
if(backStackEntry?.toRoute<Screen>() == Screen.Home){
    // do something
}
I don't know if this works yet or not. Haven't tried it recently
d
Yeah, I was hoping they had something a bit better than relying on qualifiedName (that might change one of these days... and is undocumented)
k
Also if using qualified name it might be good to verify that a build with R8 minify enabled works, if that is in use in release build. At least I had some issues, but I don’t remember if I used qualified name or something else. With the
destination._hasRoute_(FooRoute::class)
approach I haven’t had any issues.
d
@Muaz KADAN I doubt that would work unless your Screen is
@Serializable
and there's a descriminator field in the nav component's Kotlinx serialization decoder...
m
annotating screens with @Serializable as the official documentation says should be enough @dave08
There are other solutions that I found on StackOverFlow but they use Kotlin Reflect
🙃 1
Which doesn't work for me because my project is Multiplatform 😄
d
annotating screens with @Serializable as the official documentation says should be enough
You tried?
m
Yes I use it to determine when to show or not to show appBar like this
Copy code
val showAppBar: Boolean
    @Composable get() =
        BottomNavigationScreen.entries.any { bottomNavScreen ->
            currentDestination?.route == bottomNavScreen.screen::class.qualifiedName
        } || currentDestination?.route == Screen.Tasks::class.qualifiedName ||
            currentBackStack.value.any { it.destination.route == Screen.Profile::class.qualifiedName }
🫠
d
No, I meant
backStackEntry?.toRoute<Screen>() == Screen.Home
...
I tried the qualifiedName, and that actually worked, thanks!
✅ 1
I'm just not too happy to be using something undocumented...
i
You should only be using
hasRoute
, as explained in the docs: https://developer.android.com/develop/ui/compose/navigation#bottom-nav
d
Thanks! Wow, it's hidden inside those code snippets in the bottom nav docs... maybe this should also be more prominent @Ian Lake?
âž• 1
i
Where else would you like to see it except in the section where knowing what destination you are is important?
d
On the main type-safe navigation page, it would be great to see it, it's not such a long thing to explain, and in my case, I wasn't looking to migrate all my navigation yet... so I wasn't methodically looking through ALL the docs. I really wouldn't have guessed it being in that bottom bar page.. especially since the new type-safe nav is mixed in to the older string based nav docs...
i
Mmm, type safe docs should be everywhere now - where are you still seeing string based nav docs?
d
Yeah, maybe it was still on the old docs when I started, wasn't 2.8.0 released recently... but there's also the xml navs... one way or the other, only that page with type-safe was the one I concentrated on when migrating...
496 Views