0
0
Android Kotlinmobile~20 mins

NavHost and NavController in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Navigation Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you call navigate() on NavController?
Given a NavController instance, what is the immediate effect of calling navController.navigate(R.id.destinationFragment) in an Android app?
Android Kotlin
navController.navigate(R.id.destinationFragment)
AThe app closes the current activity and opens a new one.
BThe app reloads the current fragment without changing the UI.
CThe app navigates to the fragment with ID destinationFragment and adds it to the back stack.
DThe app throws a runtime exception because navigate() requires a Bundle argument.
Attempts:
2 left
💡 Hint
Think about what navigation means in a single-activity app using fragments.
lifecycle
intermediate
2:00remaining
When is NavHostFragment created in the activity lifecycle?
In an Android app using Navigation Component, at which point in the activity lifecycle is the NavHostFragment typically created and attached?
ADuring the activity's onCreate() when setContentView() inflates the layout containing NavHostFragment.
BOnly after onStart() is called on the activity.
CAfter onResume() when the UI is fully visible.
DNavHostFragment is created before the activity's onCreate() method.
Attempts:
2 left
💡 Hint
Think about when views are created from XML layouts.
📝 Syntax
advanced
2:00remaining
Identify the correct way to obtain NavController in a fragment
Which Kotlin code snippet correctly obtains the NavController inside a fragment?
Aval navController = Navigation.findNavController(activity, R.id.nav_host_fragment)
Bval navController = findNavController()
Cval navController = NavHostFragment.findNavController(this)
Dval navController = NavController(this)
Attempts:
2 left
💡 Hint
Remember the extension function available in fragments.
🔧 Debug
advanced
2:00remaining
Why does navigation crash with IllegalArgumentException?
You call navController.navigate(R.id.action_to_unknown) but the app crashes with IllegalArgumentException: navigation destination unknown. What is the most likely cause?
Android Kotlin
navController.navigate(R.id.action_to_unknown)
AThe destination ID action_to_unknown is not defined in the navigation graph XML.
BThe NavController instance is null at the time of navigation.
CThe app is missing internet permission in the manifest.
DThe fragment is not attached to the activity.
Attempts:
2 left
💡 Hint
Check your navigation graph XML for the destination ID.
🧠 Conceptual
expert
3:00remaining
How does NavHostFragment manage back stack differently than FragmentManager?
What is a key difference in how NavHostFragment manages the back stack compared to directly using FragmentManager transactions?
ANavHostFragment requires explicit calls to popBackStack(), FragmentManager does not.
BNavHostFragment does not support back stack at all, unlike FragmentManager.
CFragmentManager automatically handles deep links, but NavHostFragment does not.
DNavHostFragment uses a centralized navigation graph to manage destinations and back stack, while FragmentManager requires manual transaction management.
Attempts:
2 left
💡 Hint
Think about the role of the navigation graph in Navigation Component.