compose-destinations icon indicating copy to clipboard operation
compose-destinations copied to clipboard

Forbid navigation into subgraphs from other than start destinations of the subgraph

Open topnax opened this issue 1 year ago • 0 comments

I map my domain objects to destinations in a following way:

private fun RouterDestination.toNavigationDestination() = when (this) {
    RouterDestination.MainMenuScreen -> MainMenuScreenDestination

    is RouterDestination.TransactionSetupInitialScreen -> TransactionSetupInitialScreenDestination(
        navArgs = TransactionSetupNavArgs(terminalInfo = ...)
    )
    RouterDestination.CustomIdentifierScreen -> CustomIdentifierScreenDestination
    ...
}

Additionally, I have a custom NavGraph defined:

@RootNavGraph
@NavGraph(route = "txsetup")
annotation class TransactionSetupNavGraph(
    val start: Boolean = false
)

Finally, definition of destinations is following:

@TransactionSetupNavGraph(start = true)
@Destination(
    navArgsDelegate = TransactionSetupNavArgs::class, // mandatory arguments
)
@Composable
fun TransactionSetupInitialScreen(viewModel: TransactionSetupViewModel) {
     ...
}

@TransactionSetupNavGraph
@Destination
@Composable
fun CustomIdentifierScreen(
    viewModel: TransactionSetupViewModel,
) {
    ...
}

Is there a way to forbid navigating into a navgraph via other than start destinations?

I am currently facing a bug where if the user rapidly presses back and next buttons, the app navigates outside the current navgraph and then navigates back into the navgraph but via a non-start destination.

Since viewmodel initialization is done in the start destination, the app crashes.

topnax avatar Feb 08 '24 10:02 topnax