Android Jetpack: Introducing Navigation Component
Articles Blog

Android Jetpack: Introducing Navigation Component

August 10, 2019

around an app, by which I mean moving from one
screen to another, is an absolutely fundamental
part of Android development. Historically you’d do this
using intents or fragment transactions, which in simple
cases, like clicking a button, was easy enough. But what if you wanted to do
something just slightly more complex? For example, hooking up a common
pattern like bottom navigation. You’d need to make sure that
not only your Bottom Navigation view actually navigates
but also that it highlights the correct button. Oh, and also that it handles
the backstack in a uniform way so that users don’t end up
getting lost or confused. Cases like this are where
the new navigation component shines. The navigation component
is a collection of libraries, a
plug-in, and tooling that simplifies
Android navigation. Beyond making the setup for
common patterns like Bottom Navigation easier, the
component handles the backstack, fragment transactions,
argument passing, navigation-based animations,
and deep linking. Importantly, it gathers up all
of this navigation information and puts it in one visualized
location of your app, the Navigation graph. The navigation
component works out of the box with
fragments and activities. You can also extend the library
to work with other screen implementations, like
your custom views. Let’s look at some examples
of the navigation components in action, with a single
activity swapping out multiple fragments. If you’re wondering
why you might consider a single-activity model, check
out Ian Link’s excellent talk, which I’ve linked in the notes. OK. So one way to think about
the navigation component is that there are three
major parts working together in harmony. These are the Navigation
graph, the NavHostFragment, and the NavController. The Navigation graph
is a new resource type. It’s an XML file that contains
and centralizes information related to navigation. Now this information
can be visualized in the new Navigation editor
that is available in Android Studio 3.3. It’s a graphical
editor to create this, well, Navigation graph. Each of these screens
are called destinations, or places that you
can navigate to. In this example,
they’re all fragments. Now these arrows here
are called actions and they represent
the different paths that a user could
take through your app. If you click on
one of the actions, you can see a whole bunch
of embedded information, including data passed
between destinations, transition animations, backstack
manipulation, and so on. Clicking on a destination, you
can see things like deep link URLs and launch options. This is all part of
the XML of the graph. OK, so next, we have
the NavHostFragment. This is a fragment widget that
you’ll add to your layout, assuming that you’re
doing fragment navigation. It’s basically a window
that swaps in and out different fragment
destinations that are included in your Navigation graph. The final piece of the
puzzle is the NavController. Each NavHostFragment
has a NavController that you’ll use in your
Kotlin or Java code. And this is what actually
instructs the navigation to occur. So if I write some
code like this, the NavController will then
perform this navigation action based off of all of
that information that was in the Navigation graph,
and it will ultimately swap out which fragment is
shown in the NavHost fragment. Now here I’m using
the action’s XML ID to specify which of those
action arrows to follow, but an even better way to do
this is using the navigation component’s SafeArgs plug-in. The SafeArgs plug-in
generates code to allow you to use
type-safe navigation and argument passing. First, you’ll add the
Gradle plug-in to your code. This will generate classes based
off of your Navigation graph. It’ll make these
Direction classes for any destination
that has actions and it will make Args
classes for any destination with arguments. If you take your original
navigation statement, you can swap in these
generated classes and you’ll get something
that looks like this. Instead of referring to
an action by its XML ID, you’re making sure that
you have an action that’s actually associated
with your destination. You can also set
arguments of the action. Pass in the wrong type and
the code won’t compile. Getting the arguments you
passed through is easy. Simply use the
GeneratedArgs class and you have type-safe
access to only the correctly-named arguments. No need to worry about the
key value pairs of yesteryear. So that was simple navigation. Let’s talk about setting
up that bottom nav. The navigation
component includes an additional navigation
UI library in Java and KTX extensions for Kotlin,
that have support for options menus, bottom
navigation, navigation view, and navigation drawer. It also works with
action bar, toolbar, and collapsing toolbar. For our bottom nav,
you’d cut the bottom nav to your XML as normal. You’d also create
an XML menu with IDs that match the XML IDs
of your destinations and your Navigation graph that
you want the bottom navigation to go to. Then you’d use navigation
UI to wire up the rest. In Kotlin, that’s done with
this handy extension function. This one line of code
has NavController handle the actual
navigation, making sure to follow material
design best practices as far as backstack management goes. On top of that, it
handles highlighting the correct bottom buttons. Now we have only
explored a few parts of the navigation component. For a deeper trek
into navigation, check out the code lab
and the documentation, all linked below. Happy coding and
happy navigating.

Only registered users can comment.

  1. That's really chearing waiting for this from long time ☺️☺️☺️

  2. I think the official documentation should explain better how to deal with cases like: screens with different appbars, fragments containing other fragments, viewpagers, just to name a few…

  3. Look, we gave up on Java and created an "XML coding".
    Guess, things are not so good in Android when you need a new "suite of libraries" to highlight correct buttons.
    I wonder what Google will come up with when they decide to concentrate on fixing abominations like the file system.

  4. I haven't liked the navigation component it seems unfinished, when I was forced to use it by my company I ran into multiple issues with getting it to play nicely with collapsing toolbars inside bottom navigation views inside navigation drawer layouts, mainly with the toolbar not updating or displaying the wrong toolbar icon or a custom icon, I think they fixed a few of them but I still don't know if there's any way to check your currently shown fragment, and finding the parent when it's a XML hosted fragment is impossible there's just a lot of broken stuff in this library

  5. First of all "Navigation Component" Its perfect ,modern, easy…etc.
    But i have problem to understand lifecycle of "Navigation" why recreated view of fragment when called "findNavigation.popBack()" and not still like Activity when pressed "super.onBackPressed()" ?

  6. But sometimes there are special cases for an Activity creation, no?
    What if in one, we need immersive. In another, we hide toolbar… ?

  7. Sir my dream is become a Android developer . But I don't know how I become but I want to become Android developer at any cost.

  8. “Now, software on mobile phones is like baby software. It’s not so powerful. And today, we’re going to show you a software breakthrough. Software that’s at least 5 years ahead of what’s on any other phone. Now, how do we do this? Well, we start with a strong foundation: iPhone runs OS X.”

    Steve Jobs

  9. Is it me or Android coding today is just add more and more libraries and expect they all work out of the box as expected? If an Android developer cannot handle correctly back stack or current selection means he/she needs to improve. This libraries just make developers lazier by just adding one line of code and everything works…

  10. Hi can you help me out I want to build a OTT application for Android Mobile & tabs but I don't find anything related to this can you give me some tutorial related to this

  11. longly wanted, I programed own Navigation component that used reflection property of Java. Worked perfectly, Used it in much client projects. interesting that so much years gone before google did it

  12. Very simple and easy to follow.
    When working with the bottom navigation how do you pressing back always goes back to the start destination which may not be the destination the regardless of the previous destination. How do you handle this?

  13. Guys, I have been dealing with an issue(a requirement though) where the transaction type is replace by default. But, I need to do an add transaction to the fragments in nav graph. How can I achieve that ?

Leave a Reply

Your email address will not be published. Required fields are marked *