Challenge - 5 Problems
Button Composable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when this Button composable is clicked?
Consider this Kotlin Compose code snippet for a Button. What will be the visible result after clicking the button once?
Android Kotlin
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text(text = "Clicked: $count")
}Attempts:
2 left
💡 Hint
Remember that mutableStateOf triggers UI recomposition when changed.
✗ Incorrect
The mutableStateOf variable 'count' starts at 0. Each click increments it by 1, causing the Button's Text to update and show the new count.
📝 Syntax
intermediate2:00remaining
Which Button composable code snippet is syntactically correct?
Select the option that compiles without errors in Jetpack Compose.
Attempts:
2 left
💡 Hint
The onClick parameter requires a lambda expression.
✗ Incorrect
Option D correctly passes a lambda to onClick and a trailing lambda for the Button content. Other options have syntax errors or misplaced code.
❓ lifecycle
advanced2:00remaining
What is the effect of calling setContent inside an Activity multiple times with Button composables?
If an Android Activity calls setContent twice, each time setting a Button composable, what happens to the UI?
Attempts:
2 left
💡 Hint
setContent replaces the entire Compose UI content of the Activity.
✗ Incorrect
Each call to setContent replaces the whole UI content. So the second call removes the first Button and shows only the second.
advanced
2:00remaining
How to navigate to a new screen on Button click using Jetpack Compose Navigation?
Given a Button inside a composable, which onClick implementation correctly navigates to a "details" screen using NavController?
Android Kotlin
val navController = rememberNavController()
Button(onClick = { /* navigation code here */ }) {
Text("Go to Details")
}Attempts:
2 left
💡 Hint
Use the navigate() method with the route string to move to a new screen.
✗ Incorrect
navController.navigate("details") correctly triggers navigation to the "details" route. Other options either pop back or use invalid methods.
🔧 Debug
expert2:00remaining
Why does this Button not update its label when clicked?
Analyze the code below. Why does the Button label not change after clicking?
Android Kotlin
var count = 0 Button(onClick = { count++ }) { Text(text = "Clicked: $count") }
Attempts:
2 left
💡 Hint
Compose only updates UI when state variables change.
✗ Incorrect
The variable 'count' is a normal variable, not wrapped in mutableStateOf, so Compose does not detect changes and does not recompose the Button's Text.