In this blog, we are going to learn Jetpack Navigation, We will clarify what is Jetpack navigation, it’s benefits, and how to use it.
It simplifies implementing navigation, while also helping you visualize your app’s navigation flow. The library provides a number of benefits, including:
Automatic handling of fragment transactions so that easy to navigate between fragments.
Correctly handling up and back by default to easy the back navigation.
Default behaviors for animations and transitions for UI.
Deep linking as a first-class operation.
Implementing navigation UI patterns (like navigation drawers and bottom nav) with little additional work
Type safety when passing information while navigating
Android Studio tooling for visualizing and editing the navigation flow of an app
The Navigation component requires Android Studio 3.3 or higher and is dependent on Java 8 language features.
Overview
Overview
The Navigation Component consists of three key parts:
NavHostFragment (Layout XML view) ? This is a special widget you add to your layout. It displays different destinations from your Navigation Graph.
NavController (Kotlin/Java object) ? This is an object that keeps track of the current position within the navigation graph. It orchestrates swapping destination content in the NavHostFragment as you move through a navigation graph.
NavGrapgh (XML resource) This is the resource that contains all navigation-related information in one place so you can easily check the whole app flow in Graph. This includes all the places in the app called destination because it relates to pages in the app.
Integration
Just include the following code in the dependencies block of your module-level build.gradle file.
Here the root tag named navigation has a parameter called startDestination which has the id of our first fragment. This defines that the first fragment will be loaded in the NavHostFragment automatically.
The Navigation Component introduces the concept of a destination. A destination is any place you can navigate to in your app, usually a fragment or an activity.
Notice that for first fragment we have defined an action with the following attributes:
action
1
2
android:id="@+id/nav_first_fragment"
app:destination="@id/nav_second_fragment"
Each action should have a unique id which we will use to navigate to the required destination.
Here the destination points to the id of the second fragment defined in the nav graph, which means that with this action we will navigate to the second fragment.
After this step when you open the nav_graph.xml and switch to the design tab, it should look like the following.
Navigation types
1. Navigation using destination Id
We can provide the id of the destination fragment to navigate to it, like the following
Define the NavHostFragment. It is a special widget it will display the different destinations defined in the nav graph. To load the FirstFragment then copy the following code and paste it in the layout of the activity.
Specializes in mobile app development for e-commerce, POS, and delivery platforms. Focused on building core functionalities, resolving client issues, and leveraging modern technologies for seamless app performance.
Be the first to comment.