Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

androidx.navigation.NavGraph cannot be cast to androidx.navigation.compose.ComposeNavigator$Destination

After updating navigation compose dependency from 2.4.0-alpha03 to 2.4.0-alpha05 I've encountered this strange error after trying to navigate between composable screens (For example navigate from taskComposable to listComposable screen). Here's a part of my code.

MainActivity:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    private lateinit var navController: NavHostController

    @ExperimentalAnimationApi
    @ExperimentalMaterialApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyComposeDemoTheme {
                navController = rememberNavController()
                SetupNavigation(
                    navController = navController
                )
            }
        }
    }

}

SetupNavigation:

@ExperimentalAnimationApi
@ExperimentalMaterialApi
@Composable
fun SetupNavigation(
    navController: NavHostController
) {
    val screen = remember(navController) { Screens(navController) }

    NavHost(
        navController = navController,
        startDestination = "list/{action}"
    ) {
        listComposable(
            navigateToTaskScreen = screen.task
        )
        taskComposable(
            navigateToListScreen = screen.list
        )
    }
}

Screens:

class Screens(navController: NavHostController) {
    val list: (Action) -> Unit = { action ->
        navController.navigate("list/${action.name}"){
            popUpTo(LIST_SCREEN) { inclusive = true }
        }
    }
    val task: (Int) -> Unit = { taskId ->
        navController.navigate("task/$taskId")
    }
}

taskComposable

fun NavGraphBuilder.taskComposable(
    navigateToListScreen: (Action) -> Unit
) {
    composable(
        route = TASK_SCREEN,
        arguments = listOf(navArgument(TASK_ID_ARGUMENT_KEY) {
            type = NavType.IntType
        })
    ) {
        TaskScreen(
            navigateToListScreen = navigateToListScreen
        )
    }
}

Error:

Process: com.example.mycomposedemo, PID: 2413 java.lang.ClassCastException: androidx.navigation.NavGraph cannot be cast to androidx.navigation.compose.ComposeNavigator$Destination at androidx.navigation.compose.NavHostKt$NavHost$6$1.invoke(NavHost.kt:146) at androidx.navigation.compose.NavHostKt$NavHost$6$1.invoke(NavHost.kt:145) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215) at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84) at androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider(NavBackStackEntryProvider.kt:59) at androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider(NavBackStackEntryProvider.kt:1) at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:51) at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:50) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215) at androidx.navigation.compose.NavBackStackEntryProviderKt.LocalOwnersProvider(NavBackStackEntryProvider.kt:46) at androidx.navigation.compose.NavHostKt$NavHost$6.invoke(NavHost.kt:145) at androidx.navigation.compose.NavHostKt$NavHost$6.invoke(NavHost.kt:144) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.animation.CrossfadeKt$Crossfade$1$1.invoke(Crossfade.kt:74) at androidx.compose.animation.CrossfadeKt$Crossfade$1$1.invoke(Crossfade.kt:69) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.animation.CrossfadeKt.Crossfade(Crossfade.kt:86) at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:144) at androidx.navigation.compose.NavHostKt$NavHost$7.invoke(Unknown Source:13) at androidx.navigation.compose.NavHostKt$NavHost$7.invoke(Unknown Source:10) at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:140) at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2156) at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2399) at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2580) at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2573) at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(SnapshotState.kt:540) at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2566) at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:2542) at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:613) at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:764) at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:103) at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:447) at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:416) at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34) at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109) 2021-07-29 10:00:14.416 2413-2413/com.example.mycomposedemo E/AndroidRuntime: at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41) at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:964) at android.view.Choreographer.doCallbacks(Choreographer.java:790) at android.view.Choreographer.doFrame(Choreographer.java:721) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

like image 329
Hassa Avatar asked Jul 29 '21 08:07

Hassa


People also ask

What's new in androidx navigation compose?

androidx.navigation:navigation-compose:1.0.0-alpha07 is released. Version 1.0.0-alpha07 contains these commits. Navigation Compose now depends on Lifecycle ViewModel Compose 1.0.0-alpha01 to provide viewModel () support to composable destinations. ( I7a374)

Is it possible to create custom navtype in androidx navigation?

androidx.navigation:navigation-*:2.4.0-alpha08 is released. Version 2.4.0-alpha08 contains these commits. It is now possible to extend the NavType class to create custom NavTypes. Custom types are supported only when building your navigation graph programmatically, such as via the Navigation Graph Kotlin DSL.

How do I use compose with Android navcontroller?

To support Compose, use the following dependency in your app module’s build.gradle file: The NavController is the central API for the Navigation component. It is stateful and keeps track of the back stack of composables that make up the screens in your app and the state of each screen.

What is navgraph in Android?

androidx.navigation.NavGraph. NavGraph is a collection of NavDestination nodes fetchable by ID. A NavGraph serves as a 'virtual' destination: while the NavGraph itself will not appear on the back stack, navigating to the NavGraph will cause the starting destination to be added to the back stack.


1 Answers

This is an internal bug which will be fixed in Navigation 2.4.0-alpha06 release. @headsvk shared the issue link: https://issuetracker.google.com/issues/194301889

like image 148
M. Gasimov Avatar answered Oct 16 '22 09:10

M. Gasimov