Challenge - 5 Problems
State Hoisting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Understanding State Hoisting in Compose
What will be the visible text on the screen after clicking the button once in this Compose UI code?
Android Kotlin
var text by remember { mutableStateOf("Hello") }
Button(onClick = { text = "Clicked" }) {
Text(text)
}Attempts:
2 left
💡 Hint
Think about what happens when the button is clicked and how state changes update the UI.
✗ Incorrect
The mutableStateOf holds the current text. Clicking the button changes the text to "Clicked", which updates the UI to show "Clicked".
🧠 Conceptual
intermediate1:30remaining
Why Use State Hoisting?
Why is state hoisting recommended in Jetpack Compose components?
Attempts:
2 left
💡 Hint
Think about who should own the state for flexible UI components.
✗ Incorrect
State hoisting moves state ownership outside the component, allowing the parent to control it and making the component reusable and testable.
❓ lifecycle
advanced2:00remaining
State Hoisting and Recomposition
Given a hoisted state passed as a parameter to a composable, what happens when the state changes?
Attempts:
2 left
💡 Hint
Consider how Compose reacts to state changes passed as parameters.
✗ Incorrect
When the hoisted state changes, Compose triggers recomposition of the composable receiving it, updating the UI accordingly.
🔧 Debug
advanced2:30remaining
Identify the State Hoisting Bug
What is wrong with this Compose code that tries to hoist state but does not update the UI on button click?
Android Kotlin
var count = 0 @Composable fun Counter(count: Int, onCountChange: (Int) -> Unit) { Button(onClick = { count++ }) { Text("Count: $count") } }
Attempts:
2 left
💡 Hint
Check how state should be updated and how Compose detects changes.
✗ Incorrect
The count variable is a plain integer, not a mutable state. The onClick modifies count locally but does not notify Compose or call onCountChange. Proper state hoisting requires calling onCountChange to update the external state.
expert
3:00remaining
State Hoisting Across Navigation Destinations
In a multi-screen Compose app, where should you hoist state to preserve it across navigation between screens?
Attempts:
2 left
💡 Hint
Think about where state should live to survive screen changes and configuration changes.
✗ Incorrect
A shared ViewModel scoped to the navigation graph or activity holds state that survives navigation and configuration changes, enabling state hoisting across screens.