Challenge - 5 Problems
Bottom Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Bottom Navigation Bar Item Selection Behavior
What will happen when the user taps the second item in this BottomNavigationView setup?
Android Kotlin
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigationView.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.home -> {
showHomeFragment()
true
}
R.id.search -> {
showSearchFragment()
true
}
R.id.profile -> {
showProfileFragment()
true
}
else -> false
}
}Attempts:
2 left
💡 Hint
Look at the itemId checked in the when block and what fragment is shown for R.id.search.
✗ Incorrect
The listener checks the itemId and calls showSearchFragment() when the second item (R.id.search) is selected, returning true to indicate the event was handled. This updates the UI and highlights the Search icon.
❓ lifecycle
intermediate2:00remaining
State Preservation in Bottom Navigation
In an app with a BottomNavigationView switching between fragments, what is the best way to preserve each fragment's state when switching tabs?
Attempts:
2 left
💡 Hint
Think about how to keep fragments in memory without recreating them.
✗ Incorrect
Using add() and show()/hide() keeps fragments alive in the FragmentManager, preserving their UI and data state when switching tabs.
advanced
2:00remaining
Navigation Component with Bottom Navigation Bar
Given a BottomNavigationView integrated with Navigation Component, what is the effect of calling NavigationUI.setupWithNavController(bottomNavigationView, navController)?
Attempts:
2 left
💡 Hint
Think about how NavigationUI connects UI components with navigation actions.
✗ Incorrect
setupWithNavController links the BottomNavigationView with the NavController so that selecting items triggers navigation and the selected item updates automatically.
📝 Syntax
advanced2:00remaining
Correct Kotlin Syntax for BottomNavigationView Listener
Which option shows the correct Kotlin syntax to set a listener on BottomNavigationView that returns true when an item is selected?
Attempts:
2 left
💡 Hint
Remember the lambda syntax and that the listener expects a Boolean return.
✗ Incorrect
Option A uses a lambda with a when expression returning Boolean correctly. Option A uses invalid syntax. Option A is valid Kotlin but less idiomatic than C. Option A returns a lambda instead of Boolean.
🔧 Debug
expert2:00remaining
Debugging BottomNavigationView Item Highlight Issue
An app's BottomNavigationView does not highlight the selected item after tapping. Which code mistake causes this?
Android Kotlin
bottomNavigationView.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.home -> {
showHomeFragment()
false
}
R.id.search -> {
showSearchFragment()
false
}
else -> false
}
}Attempts:
2 left
💡 Hint
Check what the listener's return value controls in BottomNavigationView.
✗ Incorrect
Returning false means the event was not handled, so the BottomNavigationView does not update the selected item highlight.