
1. Overview
In this article, we will learn the Navigation component in Android. Navigation involves going from one app screen to another and is essential for any Android application.
Usually, you use Intent to launch another activity or fragment transaction to add or change your fragments in your current activity.
Now, Jetpack has come up with the Navigation component library to ease up things.
2. Purpose of the Navigation component
For example, assume you have a Navigation drawer view in your application that contains a list of menu items. Whenever the user clicks on any of the menu items, you load the respective content on the screen. You load different fragments on the screen for each menu.
To avoid confusion to your user and provide the best user experience, you had to consider the following points while you implement:
- Navigation to the relevant fragment
- Highlight the correct menu
- Back stack
Things are getting complicated. Isn’t it? If you don’t adhere to the above, then the user gets lost and cannot understand in which screen they are currently in. Thus, Google introduced the new Navigation component to simplify things around the application navigation.
3. Navigation component Android
The benefits of using the Navigation component are:
- Simplified setup for common navigation patterns
- Handles back stack
- Automates fragment transactions
- Type safe argument passing
- Handles transition animations
- Simplified deep linking
- Centralizes and visualizes navigation
It supports activities, fragments by default but you can extend them to work with custom views. A navigation component uses a single activity model to swap multiple fragments.
4. Prerequisite for Navigation component Android
The Navigation component requires:
- Android Studio 3.3 and above
- Java 8 language features
5. Parts of Navigation components
The Navigation component uses the following three major parts:
- Navigation graph
- NavHostFragment
- NavController
5.1. Navigation graph
The Navigation graph is a new resource type that contains and also centralizes information related to navigation. You can visualize this information using the new Navigation editor that is available in Android Studio 3.3.

This new Navigation editor is a graphical editor allowing you to edit and view the navigation graph.
You can also create nested navigation graphs and supports application deep links.

The below navigation graph contains the destinations and also actions.
- Destinations: The screens, views, or places that you can navigate to. So all activities, fragments, and custom views are destinations.
2. Actions: Refers to different paths that the user can take through the app

5.2. Navigation Host Fragment
Thee NavHostFragment is a fragment widget that you add to your layout assuming that you are doing the fragment transaction. It also acts as a window that swaps in and out the destinations declared in the navigation graph.

5.3. Navigation Controller
Each NavHostFragment contains a NavController which manages the app navigation within a NavHost
. The NavController
also performs the swapping of destination content in the NavHost
based on the information available in the navigation graph.
The NavController
allows the users to navigate either along a specific path in your navigation graph or directly to a specific destination (deep link).
6. Conclusion
To sum up, we have learned the basic concepts of the Navigation component of Android.