I am building an app where I have a top scaffold so I am changing its heade based on the route because I cannot seem to get the current Desitnation KClass.
val scaffoldListElements = remember { myAppScaffoldListElements }
val navController = LocalNavHostContoller.current
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
MyAppTheme {
val (topAppBar, bottomAppBar) = remember(currentRoute) {
val currentScaffoldDestination = scaffoldListElements[currentRoute]
Pair(
first = currentScaffoldDestination?.topAppBar,
second = currentScaffoldDestination?.bottomAppBar
)
}
Scaffold(
topBar = {
AnimatedVisibility(topAppBar != null) {
topAppBar?.invoke(navController)
}
}, ...
Routes for example:
@Serializable
data object FirstLogin {
@Serializable
data object FirstLoginScreen1
@Serializable
data class FirstLoginScreen2(val list: List<PropertyTagNavigation>)
The bar hasMap:
val myAppScaffoldListElements = buildMap {
putAll(FIRST_LOGIN_SCAFFOLD_LIST_ELEMENTS)
}.toPersistentHashMap()
the Elements:
val FIRST_LOGIN_SCAFFOLD_LIST_ELEMENTS = hashMapOf(
OwnerGraph.FirstLogin.FirstLoginScreen1::class.qualifiedName to MyAppScaffoldElement(
topAppBar = { _ ->
MyAppHeader(showBackButton = false)
}
),
OwnerGraph.FirstLogin.FirstLoginScreen2::class.qualifiedName to MyAppScaffoldElement(
topAppBar = { navHostController ->
MyAppHeader {
navHostController.popBackStack()
}
}
),
And the problem happens with the FirstLoginScreen2 since it expects a value in its route:
The route is: com.corenter.ui.OwnerGraph.FirstLogin.FirstLoginScreen2/{list}
My list’s supposed to be matching element: com.corenter.ui.OwnerGraph.FirstLogin.FirstLoginScreen2
Is there an alternative way I could approach this?
Is there some method where I could generate the matching route string for my Nav Type destinations?
You can check current NavBackStackEntry
is generated from Klass
with hasRoute(FirstLoginScreen2::class) and if it is then convert it to route with toRoute<FirstLoginScreen2>()
if (navBackStackEntry?.destination?.hasRoute(FirstLoginScreen2::class) == true) {
val route = currentNavBackStackEntry?.toRoute<FirstLoginScreen2>()
// Do something with list
route.list
}