Hi guys, I wasn't sure if <#CR4P8KNKY|> is a good ...
# compose
s
Hi guys, I wasn't sure if #CR4P8KNKY is a good place for that question, so I'm writing here. I'm migrating to type-safe Compose navigation and encountered an issue with passing arguments to a nested navigation graph. I need to pass an argument
uuid
(for example) to the start destination of a nested graph. I noticed that
navigation()
has a
typeMap
, like
composable()
, so I placed my argument there. However, I'm unsure how to retrieve the
uuid
in
AuthScreens.SignIn
. Here’s an example version of my code:
Copy code
@Serializable
@Parcelize
sealed class Screens : Parcelable {
    @Serializable data object Greeting : Screens()
    @Serializable data class Auth(val uuid: String) : Screens()
}
Copy code
@Serializable
@Parcelize
sealed class AuthScreens : Parcelable {
    @Serializable data object SignIn: AuthScreens()
    @Serializable data object ResetPassword: AuthScreens()
}
Copy code
@Composable
fun MyApp() {
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = Screens.Greeting,
        builder = {
            composable<Screens.Greeting> {
                // screen content 
                navController.navigate(Screens.Auth(uuid = "some_id"))
            }
            navigation<Screens.Auth>(
                startDestination = AuthScreens.SignIn,
                typeMap = mapOf(typeOf<String>() to NavType.StringType),
                builder = {
                    composable<AuthScreens.SignIn> {
                        // screen content
                        //TODO: how to get args ???
                    }
                    composable<AuthScreens.ResetPassword> {
                        // screen content
                    }
                }
            )
        }
    )
}
Could someone guide me on how to retrieve the
uuid
in
AuthScreens.SignIn
?
1
e
> @Serializable > @Parcelize > sealed class AuthScreens : Parcelable { > @Serializable data object SignIn: AuthScreens() > @Serializable data object ResetPassword: AuthScreens() > } First of all, you would want to use a
sealed interface
to represent your screens as you will have trouble serializing generic classes For retrieving data you can inside your navhost retrieve arguments from your backstackEntry. I’m guessing it will work something like this:
Copy code
composable<AuthScreens.SignIn> {
                        // screen content
                        //TODO: how to get args ???
val route = it.savedStateHandle.toRoute<AuthScreens.SignIn>()

// you can now access properties of the route
                    }
s
but
AuthScreens.SignIn
doesn't contains args, so
.toRoute()
will failed
just realized that I can use a separate screen for the host
NavHost
and avoid using
navigation()
altogether
z
Please keep longer code snippets to the thread
s
Sure 🤝
gratitude thank you 1