0
0
Android Kotlinmobile~20 mins

State hoisting pattern in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Hoisting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
}
AClicked
BButton
CError: Unresolved reference
DHello
Attempts:
2 left
💡 Hint
Think about what happens when the button is clicked and how state changes update the UI.
🧠 Conceptual
intermediate
1:30remaining
Why Use State Hoisting?
Why is state hoisting recommended in Jetpack Compose components?
ATo allow state to be controlled from outside the component for better reusability
BTo keep state inside the component for encapsulation
CTo prevent any state changes in the UI
DTo make the UI static and unchangeable
Attempts:
2 left
💡 Hint
Think about who should own the state for flexible UI components.
lifecycle
advanced
2:00remaining
State Hoisting and Recomposition
Given a hoisted state passed as a parameter to a composable, what happens when the state changes?
AThe app crashes due to state mismatch
BThe state resets to its initial value
CThe composable recomposes reflecting the new state value
DThe composable does not recompose because state is external
Attempts:
2 left
💡 Hint
Consider how Compose reacts to state changes passed as parameters.
🔧 Debug
advanced
2: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")
  }
}
Acount is not a mutable state, so UI does not recompose
BButton onClick should not modify count directly
ConCountChange is not called to update the state
DAll of the above
Attempts:
2 left
💡 Hint
Check how state should be updated and how Compose detects changes.
navigation
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?
AInside each composable screen individually
BIn a shared ViewModel scoped to the navigation graph or activity
CIn local variables inside the NavHost
DIn a global singleton object
Attempts:
2 left
💡 Hint
Think about where state should live to survive screen changes and configuration changes.