Working with the Navigation Component

Introduction

Android Jetpack provides the Navigation Component library, which lets us handle all the navigation scenarios in the app. It standardizes how navigation is dealt with across different components in the app and allows us to build a consistent experience.

Navigation components

  • Navigation graph: This is an XML resource file containing all navigation destinations and details of paths to reach them. Navigation graphs are specific to the activity and are defined under the res/navigation directory.

  • NavHost: This is a container that displays all fragment destinations. The library contains the NavHostFragment, which is the default implementation of the NavHost.

  • NavController: This is responsible for swapping the fragments in a NavHost.

Setting up the library

We need to add Navigation Component library dependencies in the app/build.gradle file before we can use the APIs.

Get hands-on with 1400+ tech skills courses.