Challenge - 5 Problems
State Mastery in Compose
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you use mutableStateOf without remember?
Consider this Compose code snippet:
What will happen when you click the button multiple times?
var count by mutableStateOf(0)
Button(onClick = { count++ }) {
Text("Count: $count")
}What will happen when you click the button multiple times?
Android Kotlin
var count by mutableStateOf(0) Button(onClick = { count++ }) { Text("Count: $count") }
Attempts:
2 left
💡 Hint
Think about what happens to variables declared outside remember during recomposition.
✗ Incorrect
Without remember, mutableStateOf is re-initialized on every recomposition, resetting count to 0 each time. So the UI never shows increments.
📝 Syntax
intermediate1:30remaining
Identify the correct syntax to declare a mutable state variable with remember
Which of the following Kotlin Compose code snippets correctly declares a mutable state variable named "text" initialized to an empty string using remember and mutableStateOf?
Attempts:
2 left
💡 Hint
Remember that remember is a function that takes a lambda returning mutableStateOf.
✗ Incorrect
Option D uses 'by' delegation with remember { mutableStateOf("") }, which is the correct pattern to declare a mutable state variable in Compose.
❓ lifecycle
advanced1:30remaining
How does remember behave across recompositions?
In Jetpack Compose, what is the behavior of a variable declared with remember when the composable function recomposes?
Attempts:
2 left
💡 Hint
Think about how remember helps keep state during recomposition but not beyond the composable lifecycle.
✗ Incorrect
remember stores the value in the composition and keeps it across recompositions, but resets when the composable leaves the composition.
🔧 Debug
advanced2:00remaining
Why does this Compose UI not update when state changes?
Given this code:
Why does the UI not update when the button is clicked?
@Composable
fun Counter() {
var count = remember { 0 }
Button(onClick = { count++ }) {
Text("Count: $count")
}
}Why does the UI not update when the button is clicked?
Android Kotlin
@Composable
fun Counter() {
var count = remember { 0 }
Button(onClick = { count++ }) {
Text("Count: $count")
}
}Attempts:
2 left
💡 Hint
Check if the variable is wrapped in mutableStateOf to notify Compose of changes.
✗ Incorrect
The variable count is just an integer remembered, but not a mutable state. Compose does not recompose when it changes.
🧠 Conceptual
expert2:30remaining
What is the main difference between remember and mutableStateOf in Jetpack Compose?
Choose the best explanation of the difference between remember and mutableStateOf:
Attempts:
2 left
💡 Hint
Think about what each function does in terms of state storage and recomposition triggers.
✗ Incorrect
remember keeps a value alive during recompositions; mutableStateOf creates a state object that notifies Compose to recompose when changed.