remember & mutableStateOf
Store state that survives recomposition.
remember & mutableStateOf is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is State?
In Compose, state is any value that can change over time and that your UI cares about, like a counter or a typed name. 🙂
Plain Variables Don't Work
A normal var inside a Composable changes its value, but Compose never notices, so the screen stays exactly the same.
var count = 0
Button(onClick = { count++ }) {
Text("Count: " + count)
}Meet mutableStateOf
Wrapping a value in mutableStateOf creates an observable holder. When its value changes, Compose knows it must redraw.
val count = mutableStateOf(0)
count.value = count.value + 1State Reads Are Tracked
Any Composable that reads a state's value gets subscribed to it automatically. Change the value and only those readers update.
The Recomposition Trap
Compose re-runs your function on every redraw. A fresh mutableStateOf each time would reset to zero and lose your work.
remember to the Rescue
remember stores a value across recompositions. Compose computes it once, then hands back the same instance on every redraw.
val count = remember { mutableStateOf(0) }The Classic Combo
You almost always pair them: remember keeps the holder alive, while mutableStateOf makes changes observable. Together they hold real UI state.
Cleaner with by
Use the by delegate so you read and write the value directly, skipping the noisy .value access every single time.
var count by remember { mutableStateOf(0) }
count++Putting It Together
Read count in your Text and change it in onClick. The button taps update the number, and Compose repaints just that Text.
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Tapped " + count)
}State Holds Any Type
State is not just numbers. You can hold a String, a Boolean, or a whole data class, and Compose still tracks every change.
var name by remember { mutableStateOf("") }Keep State Local for Now
For small widgets, holding state right inside the Composable is fine. Later you will lift it up, but local state is the perfect starting point.
Quick Check
Why do we wrap mutableStateOf in remember?
Recap
You learned that remember plus mutableStateOf gives you state that survives recompositions and updates the UI automatically. Nice start! 🎉
Frequently asked questions
Is the “remember & mutableStateOf” lesson free?
Yes — the full text of “remember & mutableStateOf” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.
What will I learn in “remember & mutableStateOf”?
Store state that survives recomposition. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Jetpack Compose Academy?
No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “remember & mutableStateOf” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Jetpack Compose Academy lesson?
Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- remember & mutableStateOf
- Recomposition: What Triggers a Redraw
- Build a Tap Counter
- rememberSaveable Across Rotation