0
0
Android Kotlinmobile~20 mins

Bottom navigation bar in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bottom Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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
  }
}
AThe app will display the Search fragment and highlight the Search icon in the bottom bar.
BThe app will display the Home fragment and highlight the Home icon in the bottom bar.
CThe app will crash because the listener does not handle the second item.
DNothing will happen because the listener returns false for the second item.
Attempts:
2 left
💡 Hint
Look at the itemId checked in the when block and what fragment is shown for R.id.search.
lifecycle
intermediate
2: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?
AUse finish() on the activity when switching tabs to reset state.
BUse add() and show()/hide() methods to keep fragments alive and preserve their state.
CCreate new fragment instances every time the tab is selected.
DReplace the fragment each time without adding to back stack, losing previous state.
Attempts:
2 left
💡 Hint
Think about how to keep fragments in memory without recreating them.
navigation
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)?
AIt causes the app to crash if the navController is null.
BIt disables the bottom navigation bar until manually enabled.
CIt automatically updates the selected item in the bottom bar when navigating and handles item clicks to navigate.
DIt only changes the toolbar title but does not affect navigation.
Attempts:
2 left
💡 Hint
Think about how NavigationUI connects UI components with navigation actions.
📝 Syntax
advanced
2: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?
AbottomNavigationView.setOnItemSelectedListener { item -> when(item.itemId) { R.id.home -> true else -> false } }
BbottomNavigationView.setOnItemSelectedListener(fun(item) { return true })
CbottomNavigationView.setOnItemSelectedListener { item -> { true } }
DbottomNavigationView.setOnItemSelectedListener { item -> if (item.itemId == R.id.home) true else false }
Attempts:
2 left
💡 Hint
Remember the lambda syntax and that the listener expects a Boolean return.
🔧 Debug
expert
2: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
  }
}
ANot calling notifyDataSetChanged() on the BottomNavigationView adapter.
BUsing showHomeFragment() instead of replaceFragment() causes UI not to update.
CMissing call to bottomNavigationView.invalidate() after selection.
DReturning false in the listener prevents the item from being highlighted as selected.
Attempts:
2 left
💡 Hint
Check what the listener's return value controls in BottomNavigationView.