Implement SharedViewModel in Pure Jetpack Navigation App

 

We all must have used SharedViewModel in Fragment based navigation using activityViewModels().

But now… what if our app is built in 100% Jetpack Compose? No fragments, No FragmentManager, No ViewModelStoreOwner.

So… if we have 2 basic authentication screens LoginScreen and RegisterScreen, how can we share viewmodel/state across composables? 👇🏻

// Inside any Composable screen
val parentEntry = remember(navController) {
navController.getBackStackEntry("auth_graph") // Your navGraph route
}
val viewModel: SharedViewModel = viewModel(parentEntry)

Now both LoginScreen() and RegisterScreen() inside auth_graph
can share the same SharedViewModel. ❤️

💡 Why this matters
If we’re building multi-step flows (Auth, Checkout, Onboarding), this keeps our architecture clean, and state scoped.

Checkout the Git Repo from here…
https://github.com/dev-vikas-soni/compose-shared-view-model

If you enjoyed the article and would like to show your support, be sure to:

👏 Applaud for the story (50 claps) to help this article get featured

👉Follow me on MediumLinkedIn

Check out more content on my Medium profile

Comments

Popular posts from this blog

Optimize Jetpack Compose: Performance & Best Practices

From ‘Master’ to ‘Main’: The Meaning Behind Git’s Naming Shift

Play Store Uploads with Fastlane Supply - 4