raamcosta / compose-destinations

Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
https://composedestinations.rafaelcosta.xyz
Apache License 2.0
3.14k stars 130 forks source link

Is there a way to change start destination dynamically? #485

Closed AbdullahJaved-dev closed 11 months ago

AbdullahJaved-dev commented 11 months ago

I want to change the start destination of the NavGraph/Nav Host on the basis of a condition. Currently, I have my code like this:

NavHost(
                        navController = navController,
                        startDestination = NavGraphs.root.route
                    ) {
                        authNavigation(navController)
                    }

When I try to change the start destination using If Else, the application crashes. How to fix this. Generally, I am trying to skip Splash Screen in some scenarios.

raamcosta commented 11 months ago

Hi! What’s the crash?

raamcosta commented 11 months ago

I don’t think the crash is related with the if. Rather you’re likely passing some destination which cannot be the start destination of this NavHost.

AbdullahJaved-dev commented 11 months ago

Here is the crash. Probably, I'm using something in the wrong way.

20:32:10.983 AndroidRuntime        E  FATAL EXCEPTION: main
                                      Process: com.sdsol.starfish.debug, PID: 15144
                                      java.lang.IllegalArgumentException: Start destination root cannot use the same route as the graph NavGraph(0x78dcd1ef) route=root startDestination=0x0
                                        at androidx.navigation.NavGraph.setStartDestinationRoute(NavGraph.kt:343)
                                        at androidx.navigation.NavGraph.setStartDestination(NavGraph.kt:331)
                                        at androidx.navigation.NavGraphBuilder.build(NavGraphBuilder.kt:187)
                                        at androidx.navigation.NavGraphBuilder.build(NavGraphBuilder.kt:101)
                                        at androidx.navigation.NavGraphBuilder.destination(NavGraphBuilder.kt:160)
                                        at com.sdsol.starfish.navigation.NavigationExtentionsKt.authNavigation(NavigationExtentions.kt:227)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1$1$1$1.invoke(MainActivity.kt:47)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1$1$1$1.invoke(MainActivity.kt:43)
                                        at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:376)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1$1$1.invoke(MainActivity.kt:43)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1$1$1.invoke(MainActivity.kt:35)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                        at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:132)
                                        at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:114)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                        at androidx.compose.material3.SurfaceKt.Surface-T9BRK9s(Surface.kt:111)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1$1.invoke(MainActivity.kt:32)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1$1.invoke(MainActivity.kt:31)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                        at androidx.compose.material3.TextKt.ProvideTextStyle(Text.kt:360)
                                        at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81)
                                        at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                        at androidx.compose.material3.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:73)
                                        at com.sdsol.starfish.ui.theme.ThemeKt.StarfishTheme(Theme.kt:71)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1.invoke(MainActivity.kt:31)
                                        at com.sdsol.starfish.ui.main.MainActivity$onCreate$1.invoke(MainActivity.kt:30)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                        at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:428)
                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252)
                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                        at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:186)
raamcosta commented 11 months ago

Can you show me the NavGraphs generated Kotlin file?

AbdullahJaved-dev commented 11 months ago

Got it. I was creating custom navigation. I need to pass the start destination there. It's fixed. Thank you!

fun NavGraphBuilder.authNavigation(navController: NavController, startDestination: String) {
    navigation(
        route = NavGraphs.root.route,
        startDestination = startDestination
    )