SoFunction
Updated on 2025-04-10

Android-Jetpack-Navigation component usage example

Advantages of Navigation

Some Android developers may have seen itSingle Activity Multiple FragmentofApp, very smooth or very silky to use. When you want to try this development model, you will find thatFragmentManagement will be more troublesome. Don't be afraid now,Android SDKFor usNavigationto implement this development model. I hope this article will inspire you.

Let's talk about use firstNavigationAdvantages:

  • It can visualize the navigation of the page, which can help us quickly clarify the relationship between pages;
  • passdestinationandactionComplete navigation between pages;
  • It is convenient for us to add page switching animations;
  • Type-safe parameter passing between pages;
  • passNavigationUIclass, unified management of menus, bottom navigation, and drawer menu navigation;
  • Support deep linksDeepLink(Deep link).

Navigation dependency settings for project

dependencies {
   def nav_version = "2.4.2"
   classpath ":navigation-safe-args-gradle-plugin:$nav_version"
}

Navigation dependency settings for module

plugins {
  id ''
}

Java version:

implementation ":navigation-fragment:2.4.1"
implementation ":navigation-ui:2.4.1"

Kotlin version:

implementation ":navigation-fragment-ktx:2.4.1"
implementation ":navigation-ui-ktx:2.4.1"

Compose version:

implementation ":navigation-compose:2.4.1"

The main factors of Navigation

  • Navigation Graph, a new type of XML resource file, which containsAppAll pages, and the relationship between pages.
  • NavHostFragment, specialFragmentWe can think of it as aFragmentcontainer,Navigation GraphIn-houseFragmentAll throughNavHostFragmentShow it.
  • NavController, done in the codeNavigation GraphPage switching in the work.

Navigation code example

1. Create navigation resource folder in res,navigationcreatenav_graph.xmldocument.

2. Inactivity_main.xml, The content details are as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    xmlns:tools="/tools"
    xmlns:app="/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <fragment
        android:
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name=""
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true"/>
</LinearLayout>

The attribute description is as follows:

  • android:name="", tell the system that this is a special oneFragment
  • app:defaultNavHost="true",expressFragmentThe system return key will be automatically processed, and the current return operation will be exited when the user performs the return operation.Fragment.
  • app:navGraph="@navigation/nav_graph", Navigation View.

Create two Fragments in nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:andro
    xmlns:app="/apk/res-auto"
    xmlns:tools="/tools"
    android:
    app:startDestination="@id/mainFragment">
    <fragment
        android:
        android:name=""
        android:label="fragment_main"
        tools:layout="@layout/fragment_main">
        <action
            android:
            app:destination="@id/secondFragment" />
    </fragment>
    <fragment
        android:
        android:name=""
        android:label="fragment_second"
        tools:layout="@layout/fragment_second" />
</navigation>

NavControIIer implements navigation function

We're inMainFragmentCreate button in and set the button click event:

<Button>().setOnClickListener {
   (it).navigate(.action_mainFragment_to_secondFragment)
}

This is probably the case. Interested friends can try it.

The above is the detailed content of the Android-Jetpack-Navigation component usage example. For more information about the Android-Jetpack-Navigation component, please follow my other related articles!