Skip to content

Instantly share code, notes, and snippets.

@JH108
Created September 29, 2022 20:01
Show Gist options
  • Save JH108/3b41d7ee7f9db2c7237b0ea9ad1aa1f6 to your computer and use it in GitHub Desktop.
Save JH108/3b41d7ee7f9db2c7237b0ea9ad1aa1f6 to your computer and use it in GitHub Desktop.
Compose Navigation Question

I’ve got a question on compose navigation. I know that navigation is supposed to be event based, user presses X button to go to Y screen, etc...

This is all well and good until I got to implementing more complex state with login status, whether they've onboarded, etc...

My first approach was to separate the main flows of the app into separate NavGraphs and then dynamically set the start destination of the navigation host based on state. So if the state showed the user was unauthenticated then the LoginGraph is set as the start destination, if they are authenticated but not finished onboarding then they get the OnboardingGraph.

This appeared to be working but what I hadn't realized is that when the app was first initialized the navigation host would touch at least two or three graphs before the state was fully hydrated.

I'm wondering how I would handle the scenario of needing to place the user on the right path based on some state without actually making the navgation based on state instead of events.

My current thought is to have a "root" landing page that can show a loading screen while the app "figures out" where to plop the user down, and then that navigation event would be fired based on the fully hydrated state of the app.

To put it in a list of steps:

  1. User opens app.
  2. User sees the initialization screen.
  3. The app checks various pieces of state to determine where the user should land.
  4. The AppInitialization screen uses a LaunchedEffect or similar to send the navigation "event" once the state has settled.
  5. The user now see the screen they should be on.

I had "solved" this problem previously by having a separate activity that handled authentication and would use that to start the MainActivity once the user info had been confirmed but I wanted to keep to a single activity if possible.

So the core of the question is: How would you approach placing a user on the appropriate navigation graph when launching the app after the user had completed some action?

I have a question around using multiple navigation graphs within an app. In the docs for nested navigation we see the example of home and login destinations. The question is if the startDestination should ever be state based? For example, let's say the user first landed on home, they aren't authenticated so they see a screen with a Login button. They use the button to trigger a navigation event to go to the login graph. After they do the login graph to become authenticated we send them to an onboarding graph for filling in additional information.

When does it make sense to utilize nested navigation? I have been using it for each destination for a BottomTabNavigation app but am running into some issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment