Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Graph Problem #573

Closed
Toiletman007 opened this issue Jan 30, 2024 · 6 comments
Closed

Nested Graph Problem #573

Toiletman007 opened this issue Jan 30, 2024 · 6 comments

Comments

@Toiletman007
Copy link

This is the welcome screen of our app

@Destination
@RootNavGraph(start = true)
@Composable
fun WelcomeScreen(
    navController: DestinationsNavigator
) {
setContent {
            val navHostEngine = rememberAnimatedNavHostEngine(
                navHostContentAlignment = Alignment.TopCenter,
                rootDefaultAnimations = RootNavGraphDefaultAnimations.ACCOMPANIST_FADING,
                defaultAnimationsForNestedNavGraph = mapOf(
                    NavGraphs.root to NestedNavGraphDefaultAnimations(
                        enterTransition = { slideInHorizontally() },
                        exitTransition = { slideOutHorizontally() }
                    ),
                ))
            DestinationsNavHost(navGraph = NavGraphs.root, engine = navHostEngine)
        }
}

And this is the content of our app and we have a problem. Whenever we try to navigate between NavGraphs it stops the app and after 6-7 seconds we get a android runtime exception and a ton of errors.
But if we remove DestinationsNavHost it navigates normally to MainNavGraph.

@RootNavGraph
@NavGraph
annotation class MainNavGraph(
    val start: Boolean = false
)

@MainNavGraph(start = true)
@Destination
@Composable
fun MainScreen() {

    val navController = rememberNavController()

    Scaffold(topBar = {
        MainScreenTopBar()
    }, bottomBar = {
        MainScreenBottomBar()
    }) { padding ->
        DestinationsNavHost(
            navGraph = NavGraphs.main,
            navController = navController,
            modifier = Modifier.padding(padding)
        ) {
            composable(HomeScreenDestination) {
                HomeScreen()
            }
            composable(ProfileScreenDestination) {
                ProfileScreen()
            }

        }
    }
}
@raamcosta
Copy link
Owner

Can you paste the stack trace of the exception please?

@raamcosta
Copy link
Owner

raamcosta commented Jan 30, 2024

Why is WelcomeScreen itself a Destination? 🙃

You have a destination which is the start of Root graph and then it calls DestinationsNavHost passing Root graph, which then would have another NavHost and on and on?

The place where you call DestinationsNavHost cannot it self be a destination that belongs to the graph your passing to that NavHost.
Think of DestinationsNavHost has a composable that gets replaced by screens as you navigate to them.

@Toiletman007
Copy link
Author

Oh my bad i copied the first snippet of code wrong. It's supossed to look like this

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterialNavigationApi::class, ExperimentalAnimationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navHostEngine = rememberAnimatedNavHostEngine(
                navHostContentAlignment = Alignment.TopCenter,
                rootDefaultAnimations = RootNavGraphDefaultAnimations.ACCOMPANIST_FADING,
                defaultAnimationsForNestedNavGraph = mapOf(
                    NavGraphs.root to NestedNavGraphDefaultAnimations(
                        enterTransition = { slideInHorizontally() },
                        exitTransition = { slideOutHorizontally() }
                    ),
                ))
            val navController = rememberNavController()
            DestinationsNavHost(navGraph = NavGraphs.root, engine = navHostEngine, navController = navController)
        }
    }
}

And this is the welcome screen which is start.

@RootNavGraph(start = true)
@Destination
@Composable
fun WelcomeScreen(
    navController: DestinationsNavigator
) {
   (content of welcome screen)
}

@Toiletman007
Copy link
Author

And heres the stack trace.

2024-01-30 12:26:31.045  6298-6298  AndroidRuntime          com.example                           E  	at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
                                                                                                    	at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133)
                                                                                                    	at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:98)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.placeOuterCoordinator-f8xVGno(LayoutNodeLayoutDelegate.kt:678)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.placeAt-f8xVGno(LayoutNodeLayoutDelegate.kt:656)
                                                                                                    	at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:34)
                                                                                                    	at androidx.compose.ui.layout.Placeable$PlacementScope.place(Placeable.kt:447)
                                                                                                    	at androidx.compose.ui.layout.Placeable$PlacementScope.place$default(Placeable.kt:213)
                                                                                                    	at androidx.compose.animation.AnimatedContentMeasurePolicy$measure$3.invoke(AnimatedContent.kt:837)
                                                                                                    	at androidx.compose.animation.AnimatedContentMeasurePolicy$measure$3.invoke(AnimatedContent.kt:829)
                                                                                                    	at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:70)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate$layoutChildren$1$1.invoke(LayoutNodeLayoutDelegate.kt:365)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate$layoutChildren$1$1.invoke(LayoutNodeLayoutDelegate.kt:357)
                                                                                                    	at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2299)
                                                                                                    	at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:467)
                                                                                                    	at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
                                                                                                    	at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133)
                                                                                                    	at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutSnapshotReads$ui_release(OwnerSnapshotObserver.kt:83)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.layoutChildren(LayoutNodeLayoutDelegate.kt:357)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.onNodePlaced$ui_release(LayoutNodeLayoutDelegate.kt:493)
                                                                                                    	at androidx.compose.ui.node.InnerNodeCoordinator.placeAt-f8xVGno(InnerNodeCoordinator.kt:160)
                                                                                                    	at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:34)
                                                                                                    	at androidx.compose.ui.layout.Placeable$PlacementScope.place(Placeable.kt:447)
                                                                                                    	at androidx.compose.ui.layout.Placeable$PlacementScope.place$default(Placeable.kt:213)
                                                                                                    	at androidx.compose.foundation.layout.PaddingValuesModifier$measure$2.invoke(Padding.kt:460)
                                                                                                    	at androidx.compose.foundation.layout.PaddingValuesModifier$measure$2.invoke(Padding.kt:459)
                                                                                                    	at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:70)
                                                                                                    	at androidx.compose.ui.node.LayoutModifierNodeCoordinator.placeAt-f8xVGno(LayoutModifierNodeCoordinator.kt:172)
                                                                                                    	at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:34)
                                                                                                    	at androidx.compose.ui.layout.Placeable$PlacementScope.place-70tqf50(Placeable.kt:450)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate$placeOuterCoordinator$1.invoke(LayoutNodeLayoutDelegate.kt:683)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate$placeOuterCoordinator$1.invoke(LayoutNodeLayoutDelegate.kt:678)
                                                                                                    	at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2299)
                                                                                                    	at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:467)
                                                                                                    	at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
                                                                                                    	at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133)
                                                                                                    	at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:98)

However this is only one of the stack traces as theres so many of them that the start gets overwritten and is inaccessible.

@Toiletman007
Copy link
Author

Me closing this issue was accidental, this is my first issue so i don't really know what im doing.

@raamcosta
Copy link
Owner

I don't see in the stack trace, anything related with this library, so I'm closing this. Let me know if I can help any further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants