Search code examples
androidkotlinandroid-jetpack-composeandroid-jetpack-navigation

NavHost composable accepts string as argument for startDestination, but providing serialized object is not accepted like in official post on android


I am facing an issue while using the code snippet from https://developer.android.com/guide/navigation/design#compose

@Serializable
object Profile
@Serializable
object FriendsList

val navController = rememberNavController()

NavHost(navController = navController, startDestination = Profile) {
    composable<Profile> { ProfileScreen( /* ... / ) }
    composable<FriendsList> { FriendsListScreen( / ... */ ) }
    // Add more destinations similarly.
}

the startDestination require string argument and not accepting the Profile object.

I am using "kotlinx-serialization-json" for @Serializable annotation using Profile.toString() works


Solution

  • Type safety in Kotlin DSL and Navigation Compose is available as of Navigation 2.8.0. I was using Navigation 2.7.7 which current stable release. I needed to change the version.