Stepan Revytskyi
09/23/2024, 9:42 AMuuid
(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:
@Serializable
@Parcelize
sealed class Screens : Parcelable {
@Serializable data object Greeting : Screens()
@Serializable data class Auth(val uuid: String) : Screens()
}
@Serializable
@Parcelize
sealed class AuthScreens : Parcelable {
@Serializable data object SignIn: AuthScreens()
@Serializable data object ResetPassword: AuthScreens()
}
@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
?ef
09/23/2024, 9:59 AMsealed 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:
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
}
Stepan Revytskyi
09/23/2024, 10:19 AMAuthScreens.SignIn
doesn't contains args, so .toRoute()
will failedStepan Revytskyi
09/23/2024, 12:42 PMNavHost
and avoid using navigation()
altogetherZach Klippenstein (he/him) [MOD]
09/23/2024, 4:37 PMStepan Revytskyi
09/23/2024, 4:58 PM