Navigation Architect- in Android JetPack

Tram Ho


The Navigation component, which is part of the AndroidX package, is introduced since Android SDK 28. This component includes the structure for your application, especially for navigation between fragments.

Principles of Navigation: The application should have a fixed starting point The Navigation Up function should never exit your application

Configuration build.gradle

Implement in app

** And apply **

Build UI and navigation in the app with Navigation

Creating an ACtivity with a Fragment Host tag is the master to lead to other fragments

app: navGraph: Defines the navigation file to own all the links contained in Host Fragment

app: defaultNavHost = “true”. The navigation system prevents the Back system from leaving the app when the Back button is pressed

** Destination: Destination until performing a move action ** ** The first step we create the xml Navigation file with Resource type is Navigation ** We can define an Action by dragging and dropping as follows Or you can create Action tags like the following file

** Next to rebuild app ** After that

There are 2 ways to redirect to another fragment

Pass arguments every time you change direction. For example

then in Fragment 1

Received in Fragment 2

** To pop the current Fragment we use. findNavController (). popBackStack () ** When Redirecting fragments. The navigation allows us to capture events that change direction with the destination. For example

OK, got it Above is the knowledge about switching between fragments using Navigation. Thanks for your interest and follow up. Bye !!

Share the news now

Source : Viblo