Search code examples
androidkotlinandroid-jetpack-composeandroid-navigation

Typesafe Kotlin DSL NavGraph throws IllegalStateException


I am playing with the new Kotlin DSL Navigation Compose plugin, but I get an exception:

java.lang.IllegalStateException: Cannot find startDestination com.perissf.myapp.SignIn from NavGraph. Ensure the starting NavDestination was added with route from KClass.

Composable

NavHost(
    navController = navController,
    startDestination = SignIn,
) {
    // if I remove the following lines calling NavGraphBuilder.composable() extension function, I get the exception
    composable<SignIn> {  
        SignInScreen()
    }
    signInDestination()        
}

SignInNavigation.kt

@Serializable
object SignIn

fun NavGraphBuilder.signInDestination () {
    composable<SignIn> {
        SignInScreen()
    }
}

The weird thing is that if I remove the call to the extension function NavGraphBuilder.composable, as detailed in the code comment, I get the exception; if I keep it, it works fine, but in the end it should do the same thing as the call to signInDestination() extension function written by me.

Using cutting-edge library androidx.navigation:navigation-compose:2.8.0-beta04@aarand following line by line this Android Developers guide: Encapsulate your navigation code


Solution

  • The error message indicates that the specific class, the one you use when you type startDestination = SignIn is not present in the navigation graph - i.e., there is not any composable<SignIn> that is using that exact same SignIn object.

    Make sure that you are referencing the exact same object by checking imports in your files - a good test is to move the extension method into the same file and see if that works, then try it again in its new location.