I'm learning how to use NavHost, but I don't know why Android Studio keeps telling me that NavHost doesn't have any options to use serializable objects:
None of the following candidates is applicable:
fun NavHost(navController: NavHostController, startDestination: Any, modifier: Modifier = ..., contentAlignment: Alignment = ..., route: KClass<*>? = ..., typeMap: Map<KType, @JvmSuppressWildcards() NavType<*>> = ..., enterTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = ..., exitTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = ..., popEnterTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = ..., popExitTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = ..., sizeTransform: @JvmSuppressWildcards() (AnimatedContentTransitionScope<NavBackStackEntry>.() -> SizeTransform?)? = ..., builder: NavGraphBuilder.() -> Unit): Unit
fun NavHost(navController: NavHostController, startDestination: String, modifier: Modifier = ..., contentAlignment: Alignment = ..., route: String? = ..., enterTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = ..., exitTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = ..., popEnterTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = ..., popExitTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = ..., sizeTransform: @JvmSuppressWildcards() (AnimatedContentTransitionScope<NavBackStackEntry>.() -> SizeTransform?)? = ..., builder: NavGraphBuilder.() -> Unit): Unit
fun NavHost(navController: NavHostController, startDestination: KClass<*>, modifier: Modifier = ..., contentAlignment: Alignment = ..., route: KClass<*>? = ..., typeMap: Map<KType, @JvmSuppressWildcards() NavType<*>> = ..., enterTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = ..., exitTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = ..., popEnterTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = ..., popExitTransition: @JvmSuppressWildcards() AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = ..., sizeTransform: @JvmSuppressWildcards() (AnimatedContentTransitionScope<NavBackStackEntry>.() -> SizeTransform?)? = ..., builder: NavGraphBuilder.() -> Unit): Unit
Here are the serializables:
import kotlinx.serialization.Serializable
@Serializable
object LogIn
@Serializable
data class Home(val user: String)
And here it is the navHost, the main problem:
import androidx.compose.runtime.Composable
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
@Composable
fun MainNavigation(_navController: NavController) {
NavHost(navController = _navController, startDestination = LogIn) {
compose<LogIn>{
LogInScreen()
}
}
}
I already made sure to have both navigation-compose and serializable dependencies in the project. The serializable objects are already declared correctly. In desperation I even tried to make another project from 0 but the error is still there, I seriously have no idea what to do at this point.
The problem is that there is no NavHost
function that matches the parameters you provide. It isn't the second parameter though that is problematic, it is the first.
The parameter you provide is of type NavController
, but actually required is a NavHostController
, as you can see from the error message. The latter is a specialized version of the former, for use in NavHosts. If you change your function like this is should work:
@Composable
fun MainNavigation(_navController: NavHostController) {
// ...
}
Please also note the following:
rememberNavController()
in the same composable where your NavHost is located and pass only lambdas (using the controller internally) to other composables.compose<LogIn>
must be replaced with composable<LogIn>
.object LogIn
to data object LogIn
. See here why that is recommended: https://kotlinlang.org/docs/object-declarations.html#data-objects