0
0
Android Kotlinmobile~20 mins

Navigation component setup in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Navigation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Navigation Graph XML Structure
Which option correctly defines a navigation graph XML with two fragments named HomeFragment and DetailFragment?
A<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:app=\"http://schemas.android.com/apk/res-auto\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navigation>
B<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navigation>
C<navgraph xmlns:android=\"http://schemas.android.com/apk/res/android\" app:startDestination=\"@id/homeFragment\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navgraph>
D<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:app=\"http://schemas.android.com/apk/res-auto\" app:startDestination=\"@id/homeFragment\">\n <fragment android:id=\"@+id/homeFragment\" android:name=\"com.example.HomeFragment\" />\n <fragment android:id=\"@+id/detailFragment\" android:name=\"com.example.DetailFragment\" />\n</navigation>
Attempts:
2 left
💡 Hint
Remember to include both xmlns attributes and set the startDestination with the correct fragment id.
📝 Syntax
intermediate
2:00remaining
Correct Kotlin Code to Navigate Between Fragments
Which Kotlin code snippet correctly navigates from HomeFragment to DetailFragment using Navigation component?
Android Kotlin
class HomeFragment : Fragment(R.layout.fragment_home) {
    fun goToDetail() {
        // navigation code here
    }
}
AfindNavController().navigate(R.id.action_homeFragment_to_detailFragment)
BNavigation.findNavController(this).navigate(R.id.action_homeFragment_to_detailFragment)
CfindNavController().navigate(R.id.detailFragment)
DnavController.navigate(R.id.action_homeFragment_to_detailFragment)
Attempts:
2 left
💡 Hint
Use the fragment's extension function to get NavController and navigate using the action id.
lifecycle
advanced
2:00remaining
Navigation Component and Fragment Lifecycle
What happens to the previous fragment's view when navigating to a new fragment using Navigation component with default settings?
AThe previous fragment's view is destroyed immediately after navigation.
BThe previous fragment's view remains in memory and visible behind the new fragment.
CThe previous fragment's view is replaced but the fragment instance stays alive.
DThe previous fragment's view is detached but kept in memory for quick return.
Attempts:
2 left
💡 Hint
Think about how Navigation component manages fragment views by default to save memory.
🔧 Debug
advanced
2:00remaining
Fixing Navigation Crash on Button Click
Given this Kotlin code inside a fragment, the app crashes when the button is clicked. What is the cause?
Android Kotlin
button.setOnClickListener {
    Navigation.findNavController(requireActivity()).navigate(R.id.action_homeFragment_to_detailFragment)
}
AThe action id is incorrect and does not exist in navigation graph.
BUsing requireActivity() instead of the button view causes findNavController to fail.
CThe button is not initialized before setting the listener.
DNavigation component is not added as a dependency.
Attempts:
2 left
💡 Hint
Check the parameter passed to findNavController to ensure it is a View inside a NavHostFragment.
🧠 Conceptual
expert
2:00remaining
Deep Link Handling in Navigation Component
Which statement about deep link handling in Navigation component is true?
ANavigation component does not support deep links; external libraries are needed.
BDeep links require manual intent handling in each fragment to navigate correctly.
CDeep links can be declared in the navigation graph XML and automatically navigate to the specified destination when triggered.
DDeep links only work if the app is already running in the foreground.
Attempts:
2 left
💡 Hint
Think about how Navigation component integrates with Android's intent system.