Android Jetpack: Navigation Architecture Component
Tags: android, jetpack, navigation, tutorial
Published: 2020-09-27 22:03:11

If you have been developing Android applications for some time you know that providing proper navigation between Activities and Fragments can be a time extensive and complex task. The recently released Android Jetpack Navigation Architecture Component makes this task much easier by providing many building tools needed to handle in-app navigation while also helping us visualize our app’s navigation flow.

The Navigation Component consists of three key building blocks:

1. Navigation Graph which is an XML resource that contains all navigation-related information like all off our app’s destinations or fragments and all of the possible paths our users could navigate through our app.

2. NavHostFragment which is special widget we can add to our app layout that will Host all of the destinations or fragments from our navigation graph.

3. NavController is a Kotlin object that keeps track of the user’s current position within the navigation graph and orchestrates the exchange of destination content in the NavHostFragment as our users move through the destinations in our navigation graph.

In order to illustrate how easy it is to use the Navigation Architecture Component, lets go ahead and create a new project in Android Studio.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rwkblbs3yav8z6qni3w7.png)

I will name this project NavDemo.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nhbhgav3przi3maqpwek.png)

Next we need to declare the Navigation Component’s dependencies to our app level build.gradle file. The current dependencies can be found at https://developer.android.com/jetpack/androidx/releases/navigation
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dz63upgoy1ffpcviejm6.png)

If we want to be able to pass data between fragments or destinations in our app we need to use Safe Args. In order to use Safe Args in our project we need to declare some dependencies to our project level build.gladle file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7h6fv2fjw9urgqbh9roa.png)

We also need to add a plugin to our app level build.gradle file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/62qbp0tmlm4xe9681wjg.png)

For this project we are also going to use the Android Jetpack Data Binding Library. We don’t need to use Data Binding in this project and the Navigation Component does not rely on it but is recommended as we begin to work on larger real-world projects. In order to enable data binding we just need to set dataBinding to true in the buildFeatures block of the app level build.gradle file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s3v15mxw5qdibgb89ob7.png)

In order to properly use data binding in our project we need to set layout tags to be the outermost tags in our layout xml files.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u0heec35scqdhzs5ai9t.png)

Now let create a Data Binding object in the MainActivity.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pxhugolybjsd45taw8vi.png)

Now that we have set up our project to use Data Binding and Navigation we can move forward and begin building our Navigation Graph.

First, we will create a file that will contain our navigation graph. To create this file you can right click on your app folder which will prompt a menu to pop up. Select New. Then select Android Resource File.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/akmecb81wotnj1k5xfjl.png)

A new window will pop up. Choose Navigation as the Resource Type. I will name this file nav_graph.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/waw5kf3tjns4waz7xnbd.png)

Now that the file has been created you can see that android created a new folder named navigation in within the res folder. Inside the navigation folder we will find our nav_graph file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/opvxkgubk13j2txyr0c5.png)

To host the navigation graph we need a NavHostFragment. The navigation graph connects to the other parts of the app through the NaVHostFragment. If we switch to the Design View we will see a notification that says “No NavHostFragments found”.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0fj9venwuj3afdpak7lq.png)

In order to add a NavHostFragment we nee to open our activity_main.xml file and remove the TextView.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g98n043j7200rhcskz5g.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7l4c8i199ybypszgomfh.png)

We then want to switch to Design Mode and seach for NavHostFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zh5zb27v90a2lnpmgzl7.png)

We can select the NavHostFragment and drag it into the layout prompting a pop-up to appear.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w8lbbccxvsaelgp1j7s7.png)

In this pop-up we can see that we can select the nav_graph we created earlier. Select the nav_graph option and press the OK button. Then press of the Infer Constraints button. Now we have set the activity_main as NavHostFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v6oes6lcf2zdmi6locq1.png)

If we go back to our navigation graph we can see that the activity_main is set as the NavHostFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vwudc9w1q7ywoyqrv6hk.png)

Next we have to create our Navigation Destinations. I will create a destination by clicking on the green plus button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vxvn3qqyaytotp9fa4a9.png)

A pop-up will appear with different types of fragments available. I will choose Fragment (Blank).
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6y0xk962v48sd3kq5hqu.png)

I will name this fragment HomeFragment. After clicking the Finish button, the fragment will appear in my navigation graph.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/altpe7s45lsntlj73zo4.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hclfsdmfdystagyjigqn.png)

Our layout folder now has an additional file for the homeFragment. Let’s open this file and make some small changes to the design.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dg5wflvlvvnj8fgbjvu3.png)

I will simply remove the TextView, add an EditText and a Button. Since we are using data binding we also need to add layout tags as the outer most tags in the xml and set up our binding object in our HomeFragment Class.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x6ccihgqznpimx718q34.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/60cybqu3jn5reqsu2435.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u5etqiznckb84otj3v5y.png)

Now that we have set up our Home Fragment we need to add additional destinations to our Navigation Graph by repeating the process. I will name this additional destination SecondFragment. However, this fragment will only contain a single TextView.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g7fipnp2x5ebyc3wu14e.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tut1rto43x5jn641leiu.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gzcjrups7kpgheyj50s2.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i1v35hzizu8xv205bwc1.png)

If you look at our Navigation Graph you will see that we now have two desinations, the homeFragment and the secondFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rtw4jamxx2hekp4h8is3.png)

If you run this app you will see the homeFragment is displayed, but it you click on the Submit button nothing will happen. This is because we have not set any Navigation Actions.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0o9oz6i0d9ek19df6d40.png)

To add a Navigation action from the homeFragment to the secondFragment all we have to do is click on the circular icon on the right side of the homeFragment and drag the line that appears into the secondFragment. If successful an arrow will appear that points from the homeFragment to the secondFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/de7l7e66beqpibo7ybw3.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s8yc3yudrgzd6k87ha9c.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/51klficn022ipyixj77z.png)

The same can be done if you want to create a Navigation Action from the secondFragment to the homeFragment. Android Studio automatically gives the navigation actions an id but you can change the id if you want. The id can be found in the attributes inspector.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sunucx1szhfzzteffrj8.png)

If you switch to the Text Mode in the navigation graph you will see that our navigation graph contains two fragments, the homeFragment and the secondFragment. The startDestination in our navigation is set to homeFragment. The homeFragment contains an action with a destination of secondFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xxx4xxc0o270w6ya7vuf.png)

Now we can open our HomeFragment file and set an onClickListener on our submitButton. Within this block we need to find the Navigation Controller and Navigate to our destination using the Navigation action id : action_homeFragment_toSecondFragment.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uwvqelkkdh0n83lgsff9.png)

If we run out app and press the submit button you will see that our users can now move from the homeFragment to the secondFragment. However, if you type something in the EditText and then press the submit button, you will still move from one screen to the next but the information you typed in the EditText does not get transferred to the second screen. To accomplish this we need to get the user input as a bundle and set this bundle as the second argument of the navigate function.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xk843ewc4tqm8yv7sr7a.png)

It is important to note that the best practice of passing data between destination is using a ViewModels but for this tutorial we will use a bundle as it is the Android Navigation Component allows us to use bundles to pass data.

In our secondFragment we need to receive the data from the arguments. Then we can display the data in our TextView.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3tazogej6i91mmprejdo.png)

If we run out app you will see that if you type something in the EditText and press the submit button the app will navigate to the secondFragment and display the information in the TextView.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3odn944fw8vgxj1rzdoj.png)

This was a simple example on how to use the Android Jetpack Navigation Component. There is much more you can do with it like set animations as you move from one destination to another and so much more. If you want to learn more about the Navigation Component I invite you to visit Android’s official documentation here: https://developer.android.com/guide/navigation/navigation-getting-started