Problem
Before migrating to Navigation 3, I used a single Route type with the following deeplink redirect logic:
Navigation 2 - Route.ValidatePin:
PinScreen(
onNext = {
val destination = deeplinkViewModel.getAndConsumeDestination()
if (destination != null) {
// when navigate from deeplink add Home graph as starting destination
navController.migrateAuthGraphTo(graph = Graph.Home)
navController.navigate(route = destination) {
launchSingleTop = true
}
} else {
navController.migrateAuthGraphTo(graph = Graph.Home)
}
})
I'm trying to figure out how to handle this redirect properly in Navigation 3 — especially the equivalent of navController.navigate(route = destination).
destination is the target deeplink Route returned from my view model. I can also change it so it returns the Graph that owns that Route, if that makes the Navigation 3 setup easier.
The problem is that the destination I'm redirecting to can belong to different graphs, each with its own rememberNavBackStack().
Is there a simple way to navigate to cross-graph Routes from a single place?
Project architecture
This is my project setup:
- a
@Serializabledata class/object for every navigation entry - separate sealed interfaces for
RouteandGraph
AuthGraph implementation:
@Composable
fun AuthNavigation(
modifier: Modifier = Modifier,
deeplinkViewModel: DeeplinkViewModel,
goToHome: () -> Unit,
) {
val authBackStack = rememberNavBackStack(Route.ScreensSetup)
NavDisplay(
backStack = authBackStack,
entryProvider = entryProvider {
entry {
PinScreen(
onNext = {
// same as in the code example above
},
)
}
},
)
}
NavHost implementation:
@Composable
fun MKsiadzNavHost(){
val rootBackStack = rememberNavBackStack()
NavDisplay(
backStack = rootBackStack,
onBack = { rootBackStack.removeLastOrNull() },
entryDecorators = listOf(
rememberSaveableStateHolderNavEntryDecorator(),
rememberViewModelStoreNavEntryDecorator()
),
entryProvider = entryProvider {
entry {
AuthNavigation(deeplinkViewModel =... , goToHome =...) { }
}
entry {
HomeNavigation(goToForms = {}, goToAnnouncement = {})
})}