Day60 of #100DaysOfCode

#CodeTogether Day 60/100

Hii folks 🙌

Today I will be continuing the same pathway in which I will learn to use the Navigation Graph.

Unit 3: Navigation

Pathway 2: Introduction to the Navigation component


Now, that we have familiarity with the basics of the fragments and their lifecycle, it’s time to use the Navigation Graph.

The Navigation Graph is a virtual mapping of our app’s navigation. Each screen or fragment in our case becomes a possible “destination” that can be navigated to. A NavGraph can be represented by an XML file showing how each destination relates to one another.

Use FragmentContainerView in MainActivity

Because our layouts are now contained in fragment_letter_list.xml and, fragment_word_list.xml, our activity_main.xml file no longer needs to contain the layout for the first screen in our app. Instead, we’ll repurpose MainActivity to contain a FragmentContainerView to act as the NavHost for our fragments. From this point forward, all the navigation in the app will take place within the FragmentContainerView.

  • Replace the content of the FrameLayout in activity_main.xml that is androidx.recyclerview.widget.RecyclerView with a FragmentContainerView. Give it an ID of nav_host_fragment and set its height and width to match_parent to fill the entire frame layout.
android:layout_height="match_parent" />
  • Below is the id attribute, add a name attribute and set it to androidx.navigation.fragment.NavHostFragment. While we can specify a specific fragment for this attribute, setting it to NavHostFragment allows our FragmentContainerView to navigate between fragments.

Now we will set the app:defaultNavHost="true"

and app:navGraph="@navigation/nav_graph"

Finally, we have added the two attributes with the app namespace, now we will add the xmlns:app attribute to the FrameLayout

<FrameLayout xmlns:android=""

Now we will set up the Navigation Graph and perform the navigation action.

<FrameLayout xmlns:android=""

And then configure the MainActivity

private lateinit var navController: NavController

Then after the call to setContentView() in onCreate() we will get the reference to the nav_host_fragment and assign to our navController property.

val navHostFragment = supportFragmentManager
.findFragmentById( as NavHostFragment
navController = navHostFragment.navController
  • Finally, implement onSupportNavigateUp(). Along with setting defaultNavHost to true in the XML, this method allows us to handle the up button. However, our activity needs to provide the implementation.
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()

Now, all the components are in place to get navigation working with fragments. However, now that navigation is performed using fragments instead of the intent, the intent extra for the letter that we use in WordListFragment will no longer work. In the next step, we'll update WordListFragment, to get the letter argument.

That is all for Day60 ✅

Thanks for reading, See you tomorrow!

If you are reading my #100Days Journey, feel free to drop by ;)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store