I am working on a project that utilizes Jetpack Compose Navigation with Type-Safe Navigation. Within my application, I have an composable function responsible for hosting the navigation graph. Here's a simplified version of the code:
@Composable
fun Content(
navController: NavHostController = rememberNavController()
) {
val currentBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = currentBackStackEntry?.toRoute<Route>()
NavHost(navController = navController, startDestination = Route.Route1){
composable<Route.Route1> { }
composable<Route.Route2> { }
composable<Route.Route3> { }
}
}
@Serializable
sealed interface Route {
@Serializable
data object Route1 : Route
@Serializable
data object Route2 : Route
@Serializable
data object Route3 : Route
}
I'm attempting to retrieve the current route object outside the composable
block: currentBackStackEntry?.toRoute<Route>()
. However, I encounter the following exception:
IllegalArgumentException: Polymorphic value has not been read for class null
It appears that polymorphic behavior is not supported/enabled in this context. Can someone provide guidance on how to solve this issue? I need to be able to obtain the current route object outside the NavHost composable
block using toRoute<Route>
function. Thank you!