Posts

Showing posts with the label Jetpack Compose

Implement SharedViewModel in Pure Jetpack Navigation App

Image
  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...

Optimize Jetpack Compose: Performance & Best Practices

Image
      Jetpack Compose offers a declarative approach to building UIs in Android. However, like any UI framework, optimization is key to achieving smooth performance and a great user experience. This detailed guide expands on the provided checklist, offering in-depth explanations and practical code examples for each optimization technique. I. Core Principles: Laying the Foundation for Performance Use Stable Types:  Compose relies on stable types to efficiently track changes and minimize recompositions. A stable type guarantees that if two instances of the type are equal according to  equals() , they will always be equal. Using unstable types can force unnecessary recompositions.   // Stable data class (recommended) data   class   User ( val  name: String,  val  age:  Int ) // Unstable list (avoid directly in Compose state) // Use SnapshotStateList instead val users = remember { mutableStateListOf<User>() } // Cor...