How to use Global Action in Android Component Navigation in Kotlin

In this android kotlin source code example, we are going to use Global Action in Android Component Navigation in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to use Global Action in Android Component Navigation in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Navigation.NavigationActivity16">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph16"/>

</androidx.constraintlayout.widget.ConstraintLayout>

navigation/nav_graph16.xml

<?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_graph16"
    app:startDestination="@id/navFragment16">

    <fragment
        android:id="@+id/navFragment16"
        android:name="com.bluapp.kotlinview.Navigation.NavFragment16"
        android:label="@string/app_name"
        tools:layout="@layout/fragment_nav_fragment16">
        <action
            android:id="@+id/navFragment16tonav_graph161"
            app:destination="@id/nav_graph161"/>
    </fragment>

    <navigation
        android:id="@+id/nav_graph161"
        app:startDestination="@id/navFragment161">

        <fragment
            android:id="@+id/navFragment161"
            android:name="com.bluapp.kotlinview.Navigation.NavFragment161"
            android:label="@string/app_name"
            tools:layout="@layout/fragment_nav_fragment161">
        <action
            android:id="@+id/navFragment161tonavFragment166"
            app:destination="@id/navFragment166"/>
        </fragment>

        <fragment
            android:id="@+id/navFragment166"
            android:name="com.bluapp.kotlinview.Navigation.NavFragment"
            android:label="@string/app_name"
            tools:layout="@layout/fragment_nav" />
    </navigation>

</navigation>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bluapp.kotlinview.R

class NavigationActivity16 : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation16)
    }
}

fragment_nav_fragment16.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/constraintlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Navigation.NavFragment16">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/startBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Nested Graph"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

NavFragment16.kt

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatButton
import androidx.navigation.Navigation

import com.bluapp.kotlinview.R

class NavFragment16 : Fragment() {
    private var startBtn: AppCompatButton? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_nav_fragment16, container, false)
        startBtn = view.findViewById(R.id.startBtn) as AppCompatButton
        startBtn!!.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.navFragment16tonav_graph161);
        }
        return view
    }



}

fragment_nav_fragment161.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/constraintlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Navigation.NavFragment161">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/startBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Fragment"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

NavFragment161.kt

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatButton
import androidx.navigation.Navigation

import com.bluapp.kotlinview.R

class NavFragment161 : Fragment() {
    private var startBtn: AppCompatButton? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_nav_fragment161, container, false)
        startBtn = view.findViewById(R.id.startBtn) as AppCompatButton
        startBtn!!.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.navFragment161tonavFragment162);
        }
        return view
    }


}

fragment_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Navigation.NavFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NavFragment"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

NavFragment.kt

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.bluapp.kotlinview.R


class NavFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_nav, container, false)
    }

}

app/build.gradle

    implementation 'androidx.navigation:navigation-fragment:2.1.0'
    implementation 'androidx.navigation:navigation-ui:2.1.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.