faisalahmed
11/12/2024, 8:17 AM1. Home
2. Onboarding -
* Login
* Username
* Name
* Finish Screen
The nav needs to go back to Home, from any of the nested screens in the onboarding flow.
But also, these two graphs do not share the same navController, since I have an overarching UI component, on the top of all the screens in the onboarding.
How can I achieve a clean and properly functioning Nav Graph.Stylianos Gakis
11/12/2024, 9:46 AMBut also, these two graphs do not share the same navControllerCould you explain a bit more about why that is the case? You should be able to make it all work with one NavHost as I understand your use case.
faisalahmed
11/12/2024, 11:48 AMfaisalahmed
11/12/2024, 11:49 AMStylianos Gakis
11/12/2024, 12:13 PMnavController.popUpTo<Onboarding> { inclusive = true }
on that outer NavHost? That would get rid of your entire onboarding in one go.faisalahmed
11/12/2024, 12:15 PMpopUpto
to pop the Onboarding navHost, but how do I navigate the Home NavHost to the HomeScreen then?
Will it not land on an empty screen if I just pop the nested navgraph?Stylianos Gakis
11/12/2024, 12:20 PMOnboarding
then your Home
is not on the stack?
If that is the case you can instead do this
navController.navigate(Home) {
popUpTo<Onboarding> {
inclusive = true
}
}
And it should both pop onboarding and go to Homefaisalahmed
11/12/2024, 12:21 PMHome
is not in the stack.
And yeah I was doing this, but since this navController
is a different object, than the parent Nav Graph’s, it crashes saying it doesnt recognise the destination home
.Stylianos Gakis
11/12/2024, 12:23 PMOnboarding
and Home
in different NavHosts?
You should have Home
and Onboarding
in the same one big NavHost.
Then inside the Onboarding
destination, in there you should have another NavHost, with a completely separate nav graph like this
* Login // Also make this the start destination
* Username
* Name
* Finish Screen
Stylianos Gakis
11/12/2024, 12:23 PMStylianos Gakis
11/12/2024, 12:23 PMfaisalahmed
11/12/2024, 12:24 PMOnboarding
and Home
screens are indeed in the same NavHost. The big one.
I’ll post some code for your ref.faisalahmed
11/12/2024, 12:25 PMNavHost(
navController = navController,
startDestination = startDestination
) {
composable<SpringchatScreen.OnboardingScreen> {
OnboardingRoot(
startingOnboardingLevel = onboardingLevel ?: OnboardingLevel.AUTHENTICATION
)
}
composable<SpringchatScreen.Home> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text("Home")
}
}
}
faisalahmed
11/12/2024, 12:27 PMval navController = rememberNavController()
NavHost(
navController = navController,
startDestination = startingDestination,
enterTransition = {
slideInHorizontally(
initialOffsetX = { 1000 }
)
},
exitTransition = {
slideOutHorizontally(
targetOffsetX = { -1000 }
)
},
) {
composable<OnboardingScreen.PhoneNumberAuthScreen> {
LaunchedEffect(Unit) {
currentOnboardingLevel = OnboardingLevel.AUTHENTICATION
}
PhoneNumberAuthScreen(
onNext = {
navController.navigate(SpringchatScreen.Home) {
popUpTo<SpringchatScreen.OnboardingScreen> {
inclusive = true
}
}
}
}
)
}
}
faisalahmed
11/12/2024, 12:29 PMnavController.navigate(SpringchatScreen.Home)
crashes , with the exception I mentioned above.
Which is logical since this navController doesnt know about the parent /bigger/home nav graph and its destinations like SpringchatScreen.Home
Stylianos Gakis
11/12/2024, 12:42 PMcomposable<SpringchatScreen.OnboardingScreen> {
OnboardingRoot(
startingOnboardingLevel = onboardingLevel ?: OnboardingLevel.AUTHENTICATION,
closeOnboarding = {
navController.navigate(SpringchatScreen.Home) {
popUpTo<SpringchatScreen.OnboardingScreen> {
inclusive = true
}
}
}
)
}
And then hook it up to your step which wanted to do the pop.
So if you did it from inside PhoneNumberAuthScreen
, now do:
PhoneNumberAuthScreen(
onNext = { closeOnboarding() }
)
faisalahmed
11/12/2024, 12:43 PMStylianos Gakis
11/12/2024, 12:44 PMfaisalahmed
11/12/2024, 12:45 PMStylianos Gakis
11/12/2024, 12:47 PMfaisalahmed
11/12/2024, 12:49 PMStylianos Gakis
11/12/2024, 1:09 PMStylianos Gakis
11/12/2024, 1:11 PMfaisalahmed
11/12/2024, 1:11 PMStylianos Gakis
11/12/2024, 1:17 PM.sharedElement(
on the TopAppBar, and make sure that both two screens pass the same key in the sharedElement
modifier.Stylianos Gakis
11/12/2024, 1:17 PMfaisalahmed
11/12/2024, 1:18 PM