Wiring In Navigation

In the last tutorial, we created a fragment, but we did not display it. There are three main ways we have of displaying a fragment:

In this tutorial, we will look at the third of those options.

This is a continuation of the work we did in the previous tutorial. The book’s GitLab repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

You can learn more about the Navigation component in the "Navigating Your App" chapter of Elements of Android Jetpack!

Step #1: Defining the Version

We are going to have several dependencies entries tied to the Navigation component. These will have synchronized version numbers, and we will want to use the same version number for each dependency. So, it is best to define the version number as a constant, so we can refer to that constant everywhere we need the version number. Then, when the version number changes, we can change it in one place and have it update all the necessary lines automatically.

If you open up the top-level build.gradle file — the one in the root of your project — it should resemble this:

buildscript {

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:7.0.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

Just after the opening buildscript { line, add:

  ext.nav_version = '2.3.5'

This sets up a constant that we can use in our Gradle builds files. Specifically, we are going to use a particular version of the Navigation component, and this line sets up that version number.

After making this change, you should get a banner suggesting that you “Sync Now” due to your Gradle changes. Ignore it for now, as we have more changes to make.


Prev Table of Contents Next

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