ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

How to make Jetpack Compose navigation easier and testable

Patryk Kosieradzki
ProAndroidDev
Published in
4 min readAug 8, 2021

--

Photo by Joseph Barrientos on Unsplash

Navigating in Compose is easy

Navigating in Jetpack Compose is pretty simple as you may already know. You just declare a NavHost with all your destinations and a NavHostController that will remember the navigation state and will let you controll your navigation.

For example:

We’ve just created a navController using rememberNavController() so that navigation state can survive recompositions and we declared two destinations: "firstScreen” and "secondScreen" so we can easily navigate between them.

Now to navigte from FirstScreen to SecondScreen we just use something like this:

Simple, right? But it has some drawbacks…

One last thing before we start

✉️ Android Dev Newsletter

If you enjoy learning about Android like I do and want to stay up to date with the latest, worth reading articles, programming news and much more, consider subscribing to my newsletter 👇
https://androiddevnews.com/

🎙 Android Talks Podcast

If you’re a Polish speaker and want to listen to what I have to say about Android, architecture, security and other interesting topics, check out my podcast 👇
https://androidtalks.buzzsprout.com/

The problem

While the idea is pretty simple and navigating between composables is easy, there are some problems with it:

  • Everytime you need to navigate you’ll need the navController reference. This means that if you have a really complex screen and a lot of composables down…

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Patryk Kosieradzki

Senior Android &iOS Engineer @ Moss, based in Warsaw, Poland

Responses (5)

Write a response