Day59 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the new pathway in which I will learn about the Fragments and the Navigation component.
Unit 3: Navigation
Pathway 2: Introduction to the Navigation component
Source: https://developer.android.com/courses/android-basics-kotlin/course
Fragments and the Navigation component
Android Jetpack provides the Navigation component to help us handle any navigation implementation, simple or complex, in our app. The Navigation component has three key parts which we’ll use to implement navigation in the Words app.
- Navigation Graph: The navigation graph is an XML file that provides a visual representation of navigation in our app. The file consists of destinations that correspond to individual activities and fragments as well as actions between them which can be used in code to navigate from one destination to another. Just like with layout files, Android Studio provides a visual editor to add destinations and actions to the navigation graph.
- NavHost: A
NavHost
is used to display destinations from a navigation graph within an activity. When we navigate between fragments, the destination shown in theNavHost
is updated. We'll use a built-in implementation, calledNavHostFragment
, in ourMainActivity
. - NavController: The
NavController
object lets us control the navigation between destinations displayed in theNavHost
. When working with intents, you had to call startActivity to navigate to a new screen. With the Navigation component, you can call theNavController
'snavigate()
method to swap the fragment that's displayed. TheNavController
also helps us handle common tasks like responding to the system "up" button to navigate back to the previously displayed fragment.
In the project-level build.gradle
file, in buildscript > ext
, below material_version
set the nav_version
equal to 2.3.1
.
buildscript {
ext {
appcompat_version = "1.2.0"
constraintlayout_version = "2.0.2"
core_ktx_version = "1.3.2"
kotlin_version = "1.3.72"
material_version = "1.2.1"
nav_version = "2.3.1"
}
In the app-level build.gradle
file, add the following to the dependencies group.
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Now we will perform the steps to integrate SafeArgs into our project.
In the buildscript > dependencies,
we will add the following classpath.
classpath "androidx.navigationwe :navigation-safe-args-gradle-plugin:$nav_version"
And in the app-level build.gradle
file, within plugins at the top, add androidx.navigation.safeargs.kotlin
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs.kotlin'
}
Now we will click “Sync Now” and wait a minute or two while Gradle updates your project’s dependencies to reflect the changes.
That is all for Day59 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)