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

Featured Articles

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

Android Device Security: Sandboxing, Rooting, and Attestation Explained

Play Store Uploads with Fastlane Supply - 4

Building a Production-Ready Kotlin Multiplatform Platform Kit: Lessons from My BlrKotlin x InMobi Talk

JIT vs AOT Compilation | Android Runtime