androidandroid-jetpack-composeandroid-navigationandroid-jetpack-navigation

Get current screen in Navigation Type-safety Jetpack Compose


Screens

@Serializable
sealed class Screen {
    @Serializable
    data object Home : Screen()

    @Serializable
    data class Profile(val name: String) : Screen()
}

Set up navigation

@Composable
fun AppNavigation(
    modifier: Modifier = Modifier,
    startDestination: Screen = Screen.Home,
    navController: NavHostController = rememberNavController()
) {
    val currentNavBackStackEntry: NavBackStackEntry? by navController.currentBackStackEntryAsState()

    // Error
    val currentScreen: Screen = currentNavBackStackEntry?.toRoute<Screen>() ?: Screen.Home

    val navGraph = navController.createGraph(
        startDestination = startDestination,
    ) {
        composable<Screen.Home> {
            HomeScreen(onNavigateToProfile = { navController.navigate(it) })
        }
        composable<Screen.Profile> { navBackStackEntry ->
            val profile = navBackStackEntry.toRoute<Screen.Profile>()
            ProfileScreen(name = profile.name)
        }
    }

    NavHost(navController = navController, graph = navGraph, modifier = modifier)
}

I am trying get current screen from Navigation type-safety jetpack compose. But toRoute() only use with routes in nav graph.


Solution

  • Function hasRoute() can help you with the current screen like this:

    val navBackStackEntry by navController.currentBackStackEntryAsState()
    
    navBackStackEntry?.destination?.let { currentDestination ->
        shouldShowAppBar = !currentDestination.hasRoute(Settings::class)
    }