Using JetPack No Navigation

Navigation directly translates to navigation, and is one of the Android Jetpack components that makes single-activity applications the preferred architecture. The jumping of Fragment pages within the app is handled by Navigation, eliminating the need for developers to deal with the complexity of FragmentTransaction and the associated transition animations.

Add the dependency to the app’s gradle.build.

def nav_version = "2.3.5"

// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Add the FragmentContainerView to the activity layout file.

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"/>

name is fixed and must be specified as

androidx.navigation.fragment.NavHostFragment

The defaultNavHost field indicates whether to intercept the return button operation.
If it is true, you need to override the onSupportNavigateUp method in the Activity.
By default the return key will not return to the fragment page, you can use the return key to return to the fragment page when you are done.

override fun onSupportNavigateUp(): Boolean {
    // Call the extension function findNavController
    return findNavController(R.id.nav_host_fragment).navigateUp()
}

You need to set up the navGraph navigation graph file, go ahead and create the nav_graph.xml file in the res folder, defining the relevant fragment and the respective jumping logic.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/fragment_home">

    <fragment
        android:id="@+id/fragment_home"
        android:name="com.example.navigationuse.HomeFragment"
        tools:layout="@layout/fragment_home">

        <action android:id="@+id/fragment_home_to_fragment_msg"
            app:destination="@+id/fragment_msg"/>
    </fragment>

    <fragment
        android:id="@+id/fragment_msg"
        android:name="com.example.navigationuse.MsgFragment"
        tools:layout="@layout/fragment_msg">

        <action android:id="@+id/fragment_msg_to_fragment_home"
            app:destination="@+id/fragment_home"/>

        <action android:id="@+id/fragment_msg_to_fragment_mine"
            app:destination="@+id/fragment_mine"/>
    </fragment>

    <fragment
        android:id="@+id/fragment_mine"
        android:name="com.example.navigationuse.MineFragment"
        tools:layout="@layout/fragment_mine">

        <action android:id="@+id/fragment_mine_to_fragment_msg"
            app:destination="@+id/fragment_msg"/>
    </fragment>
</navigation>

The navigation diagram implements the jumping back and forth between the 3 fragments, setting the action to be jumped. because the fragment code is simple, it will not be posted here.

Jump pages are controlled via NavController

btnHome.setOnClickListener {
    // Navigation by incoming action              
    Navigation.findNavController(btnHome).navigate(R.id.fragment_home_to_fragment_msg)
}

Jump to pass parameters

var args = Bundle()
args.putString("params", "params")
Navigation.findNavController(btnHome).navigate(R.id.fragment_home_to_fragment_msg, args)

Target page reception parameters

var params = arguments?.getString("params")

Leave a Reply