Android: Navigation Architecture. Part-3

Navigation Architecture has made it so easy to add menu based navigation.

Shubham Patni
3 min readOct 21, 2018

So far we understood basics of Navigation Architecture.

And then practical approach to implement it.

In this session we will talk about to add Menu based navigation using Architecture component. If you have ever intrgrated Navigation drawer or Bottom navigation then you might have encounter effort to set manual action to launch Fragment component and maintain menu-item background state.

Side and bottom menu navigation integration.

Using Navigation architecture it’s become like Ice on Cake {+} 😄. it’s so easy now let’s give a try.

We will be continue from last session where we have integrated Index Fragment as Home Fragment and linked Sign-in & Sign-up Fragment via navigation action.

navigation_graph file with Index Fragment implementation.

NavigationView Drawer integration: Integration guide is available here. While creating menu item id in menu.xml file all you need to do is give the same id as given destination id given in Graph file.

mapping of menu id from graph file.

And do following in your Activity file.

Navigation drawer setup with controller.

And you done 😃. No onMenu..*() method and all. It will automatic link up with your Fragment with menu action.

Bottom menu integration: It’s no more different. Steps will be same.

  1. Add BottomNavigationView using given simple guide here.
  2. While given id for item in menu xml file, referenced it with id of destination from graph file.
  3. and from Activity file attached it with Navigation Controller like following.
Bottom navigation menu setup with controller.

And you did it 😃. No additional state mantainance. Best part is in case if you have integrated both Bottom & Navigation Drawer then item state managed by Navigation component intenally. Do not need to bother about item state.

So what are advantage, let’s revise quickly one more time.

  1. Less line of code.
  2. Remove lot of boilerplate code.
  3. Manage state from graph file.
  4. Easy prototyping.

There could be situation where you need to launch another Activity, so what !! You can defined another navigation graph for that too.

In case if given graph getting complex to visualise then you can create sub-navigation group as well. All you need to do pickup component that you want’s to as sub-graph and select “Move to nested graph” and you done.

Cool !! Point to remember this is still in early state and you may find scenario per your requirement; would be useful to follow Bug Tracking here.

That’s all from here. Thank you for reading.

#HappyCoding #HappyLearning 🙌

--

--

Shubham Patni

#HumanBeing #Programmer #Android #AWS #ML #MedicalDeviceSoftware