Complete the code to navigate from one screen to another using Navigation Component.
findNavController().navigate([1])Using Navigation Component, you navigate by calling findNavController().navigate() with the action ID defined in navigation graph.
Complete the code to define a navigation action in the navigation graph XML.
<action android:id="@+id/[1]" app:destination="@id/detailFragment" />
The action ID must match the one used in code for navigation. It usually follows the pattern action_source_to_destination.
Fix the error in the code to correctly navigate with arguments.
val action = HomeFragmentDirections.[1](userId)
findNavController().navigate(action)When using Safe Args, the generated directions class uses camelCase action names matching the navigation graph action ID.
Fill both blanks to check if the current destination is the home screen before navigating.
if (findNavController().currentDestination?.id == [1]) { findNavController().navigate([2]) }
Check if the current screen is homeFragment by comparing IDs, then navigate using the correct action ID.
Fill all three blanks to create a navigation graph XML snippet with a start destination and two fragments.
<navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" app:startDestination="@id/[1]"> <fragment android:id="@+id/[2]" android:name="com.example.HomeFragment" /> <fragment android:id="@+id/[3]" android:name="com.example.DetailFragment" /> </navigation>
The start destination is the homeFragment. The two fragments must have unique IDs matching their names.