Android Navigation Component
Android Navigation Component
Here's
a breakdown of the typical workflow, assuming you're starting with a new or existing Android
project:
First, you need to include the necessary Navigation component libraries in your app's
build.gradle file. You'll typically need navigation-fragment-ktx and navigation-ui-ktx. If you're
using Safe Args for type-safe argument passing, you'll also add the Safe Args plugin.
Gradle
android {
// ...
}
dependencies {
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
// Navigation Component
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Optional: If you need dynamic feature module support
// implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
}
This is the core of your navigation. Right-click on your res directory, then select New > Android
Resource File.
● Resource type: Navigation
● File name: nav_graph (or any descriptive name)
This will create res/navigation/nav_graph.xml. Open it, and you'll see a visual editor in Android
Studio.
Every navigation graph needs a starting point. In nav_graph.xml, select the destination you want
to be the default start screen (e.g., HomeFragment) and click the "Set Start Destination" icon (a
house icon).
Your MainActivity's layout (e.g., activity_main.xml) will host the navigation graph. You use a
NavHostFragment for this.
XML
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
1
● android:name="androidx.navigation.fragment.NavHostFragment": Specifies the
NavHost.
● app:defaultNavHost="true": Ensures that the NavHostFragment intercepts system back
button presses.
● app:navGraph="@navigation/nav_graph": Links this NavHost to your navigation graph
XML.
Kotlin
// In your HomeFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.example.yourapp.R // Replace with your actual package
view.findViewById<Button>(R.id.button_to_detail).setOnClickListener {
// Option 1: Using action ID directly (less type-safe)
// findNavController().navigate(R.id.action_homeFragment_to_detailFragment)
return view
}
}
<fragment
android:id="@+id/detailFragment"
android:name="com.example.yourapp.DetailFragment"
android:label="DetailFragment"
tools:layout="@layout/fragment_detail">
<argument
android:name="someStringArg"
app:argType="string" />
<argument
android:name="someIntArg"
app:argType="integer"
android:defaultValue="0" />
</fragment>
●
●
● Rebuild Project: After adding arguments in the XML, rebuild your project (Build >
Rebuild Project). This generates the Safe Args classes.
● Kotlin
● Kotlin
import androidx.navigation.fragment.navArgs
Kotlin
// In your MainActivity.kt
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.navigation.NavigationView
import com.google.android.material.bottomnavigation.BottomNavigationView
This comprehensive approach leverages the Navigation component's features to build a robust,
maintainable, and user-friendly navigation system in your Kotlin Android app.