Are you still struggling with writing code for complex app navigation flow using Fragment transactions, Fragment Manager, back stack etc? Then Android Navigation Components is one stop solution to resolve this problem.
This Jetpack library is being vastly used by developers community across the globe and they are finding it very useful to remove the boiler plate code related to screen navigation.
The library provides the following benefits:
- Handling fragment transaction management with ease
- It provides visualization of application flow through navigation graph
- Automatic back and up navigation management.
- Provides implementation for animations and transitions
- It implements and handles deep linking
- Navigation UI patterns like menu navigation, navigation drawers and bottom nav with less efforts
- Type safety when passing information while navigating.
Steps to Use :
1. Add dependencies :
2. Create a navigation graph:
The Navigation Graph is a visual flow that describes a group of Navigation destinations and their connections.
Add new file : res -> navigation -> nav_graph.xml
Code view of nav_graph.xml:
Parameters provided with Navigation components :
Destination : Each destination represents a screen user can navigate to.
Action : Defines the route between one destination to another.
SafeArgs : Arguments that can be passed from one destination to another.
Animation : Screen transition animation while navigating from one destination to another.
Deep Linking : A concept that help users to navigate between the applications and web through URLs which navigate users directly to the specific content in applications(Nice article here).
With Navigation Component you can use a URI instead of an action to navigate.
3. Add a Navigation Host to an activity
One of the core parts of the Navigation component is the Navigation Host that need to be added (NavHostFragment here) in the activity_main layout.
This binds the navigation graph(nav_graph) to the activity with the fragment mentioned as “app:startDestination”.
4. Navigate between Destinations.
Navigating to a destination is done using a
NavController, an object that manages app navigation within a NavHost. Something like this:
5. Implementing the Bottom Navigation Bar using Navigation Component .
Destinations defined in the navigation graph can be tied to menu items of bottom navigation view as follows.
That is all folks. So, Android Jetpack’s Navigation Component is an ultimate solution to create and visualize your complete app navigation between the fragments. Therefore, it’s a time to say good bye to boilerplate code involving Fragment Managers and Fragment Transactions.
Please find the full source code of the sample app in the Github. I would recommend to watch YouTube video on the same from CodingWithMitch .
You can find the Article on my Medium blog as well.
https://github.com/amritlalsahu5/AndroidNavigationComponents https://codelabs.developers.google.com/codelabs/android-navigation https://www.youtube.com/watch?v=JFGq0asqSuA https://www.youtube.com/watch?v=IEO2X5OU3MY&t=3032s