Elements of Navigation

There are four main pieces of the Navigation component:

Navigation Resources

Google only infrequently creates a new type of resource, and almost never do they create one that is not handled directly by the OS.

For the Navigation component, they did just that.

You can have a res/navigation/ directory in your module that holds navigation resources. Those are XML files with a root <navigation> element and a series of child elements that describe the navigation graph of your app.

Principally, the Navigation component handles navigation between fragments within an activity. Hence, the primary children of the <navigation> element are <fragment> elements, identifying a particular fragment that is part of the navigation graph. However, if you want to navigate to a different activity — perhaps one that has its own navigation graph — you can have child <activity> elements off of <navigation> that identify those.

The <fragment> and <activity> elements — otherwise known as the “destinations” — have a number of possible child elements, of which two are the most important:

The root <navigation> element will have an app:startDestination attribute that identifies which <fragment> should be displayed when this navigation graph is used by an activity.

We will explore all of these in greater detail when we work through a sample app a bit later in the chapter.

Navigation Resource Editor

You are welcome to maintain navigation resources manually by editing the XML. Or, you can use Android Studio’s dedicated editor for those resources.

As with layout and menu resources, the navigation editor is a visual drag-and-drop editor, or you can work with the XML directly.

Destinations

The main “preview” area shows the existing destinations in the navigation graph:

Navigation Editor Destination Preview
Navigation Editor Destination “Preview”

To add a new destination, you can click the left-most toolbar button in the preview area:

Navigation Editor Preview Area Toolbar
Navigation Editor Preview Area Toolbar

This will bring up a list of all of the activities and fragments in your module, along with options for a “placeholder” and “create new destination”. The latter brings up a new-fragment wizard to help you create a new fragment.

Navigation Editor Add Destination Drop-Down
Navigation Editor “Add Destination” Drop-Down

If you click on a particular destination, the “Attributes” pane will show you key information about that destination, much of which is editable:

Navigation Editor Destination Attributes
Navigation Editor Destination Attributes

Of particular note:

Navigation Editor Add Argument Dialog
Navigation Editor “Add Argument” Dialog

In the preview area toolbar, you can click the “home” icon to designate the selected destination as being the start destination for this navigation graph.

Actions

Arrows connecting destinations in the preview area represent actions. Reminiscent of constraining widgets in a ConstraintLayout, you can drag a circle on the right edge of a destination to a corresponding circle on the left edge of another destination to create an action. This indicates that you want to be able to navigate from the first destination to the second.

Clicking on an arrow highlights it in blue and brings up its own set of attributes:

Navigation Editor Action Attributes
Navigation Editor Action Attributes

Of particular note:

NavHostFragment

As noted earlier, the primary focus of the Navigation component is to navigate between fragments within an activity. This implies that the Navigation component knows where those fragments should go.

That is in the form of a NavHostFragment. This is a fragment that shows other fragments, the ones specified in your navigation graph. You tell it what navigation graph it should use, and it will be responsible for:

NavController

Switching to another destination in your navigation graph at runtime involves a NavController. This will be associated with the activity or fragment that is hosting the NavHostFragment. There are utility functions for getting the NavController, and from there you can ask it to move forwards or backwards in the navigation graph. “Forwards” means “go to this destination”; “backwards” means “go back to where we came from to get to our current destination”.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.