The Navigation component helps you move between screens in your app easily and safely. It manages the back stack and animations for you.
Navigation component setup in Android Kotlin
1. Add Navigation dependencies in build.gradle: implementation "androidx.navigation:navigation-fragment-ktx:2.5.3" implementation "androidx.navigation:navigation-ui-ktx:2.5.3" 2. Create a navigation graph XML (res/navigation/nav_graph.xml): <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph" app:startDestination="@id/firstFragment"> <fragment android:id="@+id/firstFragment" android:name="com.example.FirstFragment" android:label="First Screen" /> <fragment android:id="@+id/secondFragment" android:name="com.example.SecondFragment" android:label="Second Screen" /> </navigation> 3. Setup NavHostFragment in your activity layout: <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true" /> 4. Use NavController in your activity or fragment to navigate: val navController = findNavController(R.id.nav_host_fragment) navController.navigate(R.id.secondFragment)
The navigation graph XML defines all screens and how to move between them.
NavHostFragment is a container that shows the current screen.
navController.navigate(R.id.secondFragment)
app:startDestination="@id/homeFragment"<fragment
android:id="@+id/profileFragment"
android:name="com.example.ProfileFragment"
android:label="Profile" />This example shows how to set up the Navigation component with two screens. The app starts at First Screen and immediately navigates to Second Screen.
/* build.gradle (Module) dependencies */ implementation "androidx.navigation:navigation-fragment-ktx:2.5.3" implementation "androidx.navigation:navigation-ui-ktx:2.5.3" /* res/navigation/nav_graph.xml */ <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph" app:startDestination="@id/firstFragment"> <fragment android:id="@+id/firstFragment" android:name="com.example.FirstFragment" android:label="First Screen" /> <fragment android:id="@+id/secondFragment" android:name="com.example.SecondFragment" android:label="Second Screen" /> </navigation> /* res/layout/activity_main.xml */ <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true" /> /* MainActivity.kt */ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import com.example.R class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navController = findNavController(R.id.nav_host_fragment) // Navigate to secondFragment after 2 seconds (simulate user action) navController.navigate(R.id.secondFragment) } }
Always add the Navigation dependencies in your build.gradle file.
Use the Navigation Editor in Android Studio to visually create and edit the navigation graph.
NavController handles the back button automatically for you.
The Navigation component helps manage screen changes easily.
Create a navigation graph XML to define screens and paths.
Use NavHostFragment in your layout to show screens.
Use NavController to move between screens in code.
Practice
Solution
Step 1: Understand Navigation component role
The Navigation component is designed to simplify moving between screens in an app.Step 2: Compare with other options
Database, styling, and network are unrelated to navigation tasks.Final Answer:
To manage screen transitions and navigation paths easily -> Option AQuick Check:
Navigation component = screen transitions [OK]
- Confusing navigation with database handling
- Thinking it styles UI elements
- Assuming it manages network calls
Solution
Step 1: Identify the navigation graph element
The navigation graph XML uses the <navigation> root element to define screens and paths.Step 2: Check other elements
<navHost> and <navController> are not XML tags; <navGraph> is incorrect.Final Answer:
<navigation> -> Option BQuick Check:
Navigation graph XML uses <navigation> [OK]
- Using <navGraph> which is invalid
- Confusing NavHostFragment with XML tag
- Mistaking NavController for XML element
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.HomeFragment" />
<fragment
android:id="@+id/detailFragment"
android:name="com.example.DetailFragment" />
</navigation>Solution
Step 1: Identify startDestination attribute
The navigation graph sets startDestination="@id/homeFragment", so HomeFragment is the first screen.Step 2: Confirm fragment IDs
HomeFragment and DetailFragment are defined; startDestination points to HomeFragment.Final Answer:
HomeFragment -> Option CQuick Check:
startDestination = HomeFragment [OK]
- Choosing DetailFragment by mistake
- Assuming no screen shows without NavHostFragment
- Confusing fragment IDs
findNavController().navigate(R.id.action_home_to_detail)What is the most likely cause?
Solution
Step 1: Check NavHostFragment presence
Without NavHostFragment in layout, navigation cannot work and causes crash.Step 2: Verify action ID and context
If action ID is wrong or findNavController() is called outside proper context, app crashes.Final Answer:
All of the above -> Option AQuick Check:
Missing NavHost, wrong ID, or wrong context cause crash [OK]
- Ignoring NavHostFragment setup
- Using wrong action ID
- Calling findNavController() in wrong place
Solution
Step 1: Define fragments and action in navigation graph
Both fragments and the action connecting them must be declared in nav_graph.xml.Step 2: Add NavHostFragment in activity layout
NavHostFragment hosts the navigation and displays fragments.Step 3: Use findNavController().navigate(actionId) in code
This triggers navigation along the defined action.Final Answer:
Define fragments and action in nav_graph.xml, add NavHostFragment in layout, call findNavController().navigate(actionId) -> Option DQuick Check:
Fragments + action + NavHost + navigate() = correct setup [OK]
- Skipping NavHostFragment in layout
- Not defining action in nav_graph.xml
- Calling navigate() without proper setup
Ready to prove it?
Pass the test and this pattern moves unknown
