💡 Mastering State in Jetpack Compose: Let the UI React to You!


✨ Introduction
One of the biggest mindset shifts when moving from traditional XML-based Android UI to Jetpack Compose is how we manage state and UI updates. In the Compose world, we no longer tell the UI how to update — we just say what we want it to be, and Compose handles the rest!
In this post, we’ll explore:
Why managing state is key in Jetpack Compose
The difference between XML and Compose in handling UI updates
Practical ways to maintain state using
mutableStateOf
,remember
, andrememberSaveable
🧠 From XML to Compose: What Instead of How
In XML-based UI, when a user interacted with the app (like selecting an item or rotating the phone), you had to manually update the UI — write logic to remember the user's action and redraw the screen accordingly.
But with Jetpack Compose, you shift your focus to declaring the UI based on state. Compose automatically recomposes the UI when that state changes.
📱 Example:
If a user selects an answer and rotates the screen:
In XML: You need to manually save the selected answer and restore it.
In Compose: You just maintain the right state variable, and Compose will recompose the UI automatically!
That’s why in Compose, we don’t control the UI flow — we just define what the UI should look like for a given state.
🔄 How to Change the UI and Maintain the State?
Let’s walk through the three main approaches to maintaining state in Jetpack Compose — with pros and cons.
🧪 1. Basic State with mutableStateOf()
var varname: MutableState<YourType?> = mutableStateOf(null)
varname.value = parameter
✅ Pros:
- Lets you update the UI based on user interactions.
❌ Cons:
- Will not survive recompositions — meaning the state might reset unexpectedly.
🧠 2. Remembering State with remember {}
var varname: MutableState<YourType?> = remember { mutableStateOf(null) }
✅ Pros:
- Keeps your state even when recomposition happens.
❌ Cons:
- Doesn't survive configuration changes like screen rotation.
💾 3. Save State Across Configuration Changes with rememberSaveable
var varname: YourType? by rememberSaveable { mutableStateOf(null) }
✅ Pros:
Survives both recompositions and configuration changes.
Cleaner syntax with
by
keyword — no more.value
access needed!
ElevatedButton(onClick = { expanded = !expanded }){
// Text(if (expanded.value) "Show less" else "Show more") // no need to access the var with .value
Text(if (expanded) "Show less" else "Show more") // you can directly access it.
}
⚡ With rememberSaveable
, Compose will automatically restore your state even after rotation. Ideal for quiz apps, forms, and interactive UI!
🏁 Conclusion
State management in Jetpack Compose is not just a feature — it's the foundation of building reactive, modern UIs. Instead of managing every UI change manually like in XML, Compose reacts to your state changes.
To recap:
Use
mutableStateOf()
for quick UI interactions.Wrap it in
remember {}
to survive recompositions.Use
rememberSaveable
to persist state across configuration changes.
🚀 Ready to try this out?
Start replacing your XML logic with Compose and experience the power of declarative UI!
🔗 Resources
Subscribe to my newsletter
Read articles from Tejas Kanazriya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
