0:0037:53

Basics of Android Jetpack Navigation Components

Peculiar C Umeh 37:53 2021

Android navigation can live in one graph instead of scattered intents

Moving from one screen to another can be as simple as an intent on a button click, until an app grows complex. Android developer and technical writer Peculiar C Umeh builds a navigation graph, a NavHost and actions from an empty Android Studio project, showing how much of the wiring the tooling writes for you. What does it take to set up Jetpack Navigation by hand and pass data between screens?

Follow a step-by-step build of a nav graph, NavHost, actions and Safe Args arguments

Watch on YouTube

In short

Android developer Peculiar C Umeh introduces the Jetpack Navigation component, explaining its navigation graph, NavHost and NavController and why it beats wiring screen changes by hand with intents. The talk then builds a navigation graph, host, fragments, actions and a typed argument live in Android Studio, and shows Safe Args passing an ID in a sample drinks app.

“It simply means moving from one screen to another.”
“But you might not need to do that because once you want to generate your nav graph, it will ask you if you need this dependency in your app.gradle file.”

What you will learn

  • Explain the three parts of Jetpack Navigation: the navigation graph, the NavHost and the NavController.
  • Create a navigation graph resource file in Android Studio and let it add the navigation dependency to the app Gradle file.
  • Add a NavHost to an activity layout, link it to the graph and set a start destination for a single-activity app.
  • Connect destinations with actions and add typed arguments in the navigation graph editor.
  • Use the Safe Args Gradle plugin, which generates Directions and Args classes, to pass data such as an ID between screens.

Key takeaways from the speaker

  • In this session, we will go through how to implement a navigation graph manually and how to navigate to different destinations. We will also look on how to pass data between destinations using safeArgs and a bit of deep linking if time permits.
  • Using best practice
  • Tools needed are Personal Computer, Install Android Studio latest version

Details

Level
Beginner
Format
Tech talk
Technical depth
Practitioner
For
Student, Early career
Length
37:53
Published
June 2021

Questions this talk answers

What is the Jetpack Navigation component?

It is a plugin and tooling for simplifying and unifying Android navigation. It is made up of three main parts: the navigation graph, the NavHost and the NavController.

Why use Jetpack Navigation instead of intents on button clicks?

It handles the back stack and transition animations, automates fragment transactions, simplifies deep linking and handles type-safe argument passing. It also helps set up navigation UI patterns in a simplified way.

How do you create a navigation graph manually?

Right-click the resource folder, choose New, Android Resource File, name it nav_graph and select Navigation as the resource type. Android Studio then offers to add the navigation dependency to the app Gradle file.

How do you pass data between destinations?

Add the Safe Args plugin to the Gradle file; it generates a Directions class for destinations with actions and an Args class for destinations with arguments. Arguments are added in the graph editor with an ID and a type such as string, float or boolean.

Does Jetpack Navigation work in Java or only Kotlin?

It works in both, but the speaker prefers Kotlin because the code is more concise, for example bottom navigation can be wired with one line of code.

About this talk

Jetpack navigation library was created to provide a better user experience with navigation. However, most developers do not know how to harness the full power of the library. This session is here to help you get a deeper understanding of the library and what it offers.

Topics

Related talks

Browse the library

Why Jetpack Compose?

Miriana Itani Android Team Lead

A Compose counter that never counts, and what it teaches about state

Intermediate Software engineering and cloud 2022