Posts

SavedStateHandle vs. rememberSaveable: Which One Should You Choose?

Image
 A senior developer's guide to clean state management in Jetpack Compose—where to store your data and why it matters for app stability. TL;DR: SavedStateHandle:  Use for  Business/Presentation State  (IDs, search queries) in the ViewModel. It is the most reliable anchor for process restoration. rememberSaveable:  Use for  Pure UI State  (scroll position, animation toggles) in the Composable. Rule of Thumb:  If losing the state breaks the screen’s logic, use the ViewModel. If it only resets a visual preference, keep it in the UI. 1. The Architecture Gap In a modern Jetpack Compose app, state lives in two distinct layers. Choosing the wrong bucket leads to “State Leakage,” where your business logic becomes tangled with your UI or your UI becomes unresponsive after a background kill. SavedStateHandle (The ViewModel Layer) This is your  Architectural Anchor . It allows the ViewModel to interact with the system’s saved instance state. Because the ...

Your ViewModel Won't Save You: Designing for Android Process Death

Image
  How to use SavedStateHandle and the Single Source of Truth pattern to build "death-proof" apps that never lose user progress. TL;DR: ViewModel  survives rotations, but  not  process death. LMK (Low Memory Killer)  will kill your background app to reclaim RAM. SavedStateHandle  is your tool for restoring small UI states (IDs, flags). Room/DataStore  is required for critical business data persistence. Test  using  adb shell am kill <package>  to see your app's true resilience. The Ghost in the Machine You’re in a banking app, halfway through a wire transfer. You switch to your email to copy an OTP. When you return, the app has reset to the login screen, and your progress is gone. To the user, this is a “glitch.” To a  seasoned Android developer , this is a failure to architect for  Process Death . Android enforces strict process management regardless of available RAM. If you want to build professional software, you must s...