State with remember and mutableStateOf
Hold UI state in composables.
State with remember and mutableStateOf is a free Android 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 Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is State?
State is any value that can change over time and that the UI should react to — a counter, a text field's contents, whether a checkbox is on. When state changes, Compose redraws.
mutableStateOf
mutableStateOf creates an observable value. When you write to it, Compose knows to re-run any composable that read it.
val count = mutableStateOf(0)
// read with count.value, write with count.value = 1Why remember?
Composables can run many times (recomposition). If you create state on every run, it resets every time. remember tells Compose to keep the value across recompositions.
val count = remember { mutableStateOf(0) }The by Delegate
Reading and writing .value everywhere is noisy. Kotlin's by property delegate lets you treat state like a normal variable.
var count by remember { mutableStateOf(0) }
// now use count directly, no .valueNeeded Imports
To use the by delegate you import the getter/setter helpers. Android Studio adds these automatically.
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberA Counter Composable
Here is a working counter. Tapping the button increments count, and the Text updates automatically.
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text(text = "Count: $count")
}
}Read Triggers Update
The magic: the Text reads count. Compose records that read, so when count changes, only that part recomposes and shows the new value.
State Holds Any Type
State is not just for numbers. You can hold a String, a Boolean, a list, or your own data class.
var name by remember { mutableStateOf("") }
var isOn by remember { mutableStateOf(false) }TextField with State
An editable text field needs state for its current value plus a callback to update it.
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it }
)State Is Local
State created with remember lives inside the composable. Two calls to Counter() each have their own independent count.
Full Example
A complete counter screen combining everything from this lesson.
@Composable
fun CounterScreen() {
var count by remember { mutableStateOf(0) }
Column {
Text(text = "You tapped $count times")
Button(onClick = { count++ }) {
Text(text = "Tap")
}
}
}Quick Check
Why do we wrap mutableStateOf in remember?
Recap
State basics:
mutableStateOfmakes an observable value.rememberkeeps it across recompositions.bylets you skip.value.- Reading state subscribes a composable to its changes.
Frequently asked questions
Is the “State with remember and mutableStateOf” lesson free?
Yes — the full text of “State with remember and mutableStateOf” is free to read here on the web, and the Android 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 Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “State with remember and mutableStateOf”?
Hold UI state in composables. You practise Android 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 Android Academy?
No prior experience is required. Android 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 “State with remember and 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 Android Academy lesson?
Yes. Every Android 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
- State with remember and mutableStateOf
- Recomposition Explained
- State Hoisting
- rememberSaveable