Android: Navigation Architecture. Part-3
Navigation Architecture has made it so easy to add menu based navigation.
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.
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.
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.
And do following in your Activity file.
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.
- Add BottomNavigationView using given simple guide here.
- While given id for item in menu xml file, referenced it with id of destination from graph file.
- and from Activity file attached it with Navigation Controller like following.
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.
- Less line of code.
- Remove lot of boilerplate code.
- Manage state from graph file.
- 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 🙌