Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a NavHostFragment in the activity layout.
Android Kotlin
val navHostFragment = supportFragmentManager.findFragmentById(R.id.[1]) as NavHostFragment
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect fragment ID that does not exist in the layout.
Confusing the NavHostFragment ID with other container IDs.
✗ Incorrect
The NavHostFragment is usually referenced by its ID in the layout, commonly named 'nav_host_fragment'.
2fill in blank
mediumComplete the code to navigate to a nested graph destination using NavController.
Android Kotlin
navController.navigate(R.id.[1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to navigate directly to a fragment inside the nested graph without specifying the nested graph ID.
Using a fragment ID instead of the nested graph ID.
✗ Incorrect
To navigate to a nested navigation graph, you use the ID of the nested graph itself, not a fragment destination.
3fill in blank
hardFix the error in the nested graph XML by completing the missing attribute.
Android Kotlin
<navigation xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/[1]" app:startDestination="homeFragment">
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same ID as the main graph causing conflicts.
Omitting the android:id attribute entirely.
✗ Incorrect
The nested graph must have a unique ID, commonly named 'nested_graph' to distinguish it from the main graph.
4fill in blank
hardFill both blanks to define a nested navigation graph with a start destination.
Android Kotlin
<navigation android:id="@+id/[1]" app:startDestination="[2]">
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the graph ID with fragment IDs.
Setting a start destination that does not exist in the nested graph.
✗ Incorrect
The nested graph ID is 'nested_graph' and its start destination is 'homeFragment'.
5fill in blank
hardFill all three blanks to correctly retrieve the NavController from a nested NavHostFragment.
Android Kotlin
val navHostFragment = supportFragmentManager.findFragmentById(R.id.[1]) as [2] val navController = navHostFragment.[3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong fragment ID.
Casting to the wrong fragment type.
Trying to access navController without casting.
✗ Incorrect
You find the nested NavHostFragment by its ID 'nested_nav_host', cast it to NavHostFragment, then get its navController property.