Navigation Component for app navigation

Alex Chen Jan 2026
3 tabs
<?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/postsFragment">

    <fragment
        android:id="@+id/postsFragment"
        android:name="com.example.myapp.ui.posts.PostsFragment"
        android:label="@string/posts_title"
        tools:layout="@layout/fragment_posts">

        <action
            android:id="@+id/action_posts_to_detail"
            app:destination="@id/postDetailFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right" />

        <action
            android:id="@+id/action_posts_to_create"
            app:destination="@id/createPostFragment" />
    </fragment>

    <fragment
        android:id="@+id/postDetailFragment"
        android:name="com.example.myapp.ui.detail.PostDetailFragment"
        android:label="@string/post_detail_title"
        tools:layout="@layout/fragment_post_detail">

        <argument
            android:name="postId"
            app:argType="integer" />

        <argument
            android:name="title"
            app:argType="string"
            app:nullable="true" />

        <deepLink
            app:uri="myapp://post/{postId}" />
    </fragment>

    <fragment
        android:id="@+id/createPostFragment"
        android:name="com.example.myapp.ui.create.CreatePostFragment"
        android:label="@string/create_post_title"
        tools:layout="@layout/fragment_create_post" />

    <fragment
        android:id="@+id/profileFragment"
        android:name="com.example.myapp.ui.profile.ProfileFragment"
        android:label="@string/profile_title"
        tools:layout="@layout/fragment_profile" />
</navigation>
3 files · xml, kotlin Explain with highlit

Navigation Component provides consistent navigation with type-safe argument passing. I define navigation graph in XML with destinations and actions. NavController handles navigation with navigate() and popBackStack(). Safe Args plugin generates type-safe classes for passing arguments between destinations. Deep links connect external URLs to destinations. Navigation UI integrates with Toolbar, BottomNavigationView, and DrawerLayout. Single Activity architecture simplifies lifecycle management. Nested graphs organize complex flows. Conditional navigation uses NavOptions for animations and pop behavior. The component handles Fragment transactions automatically, preventing common bugs. Compose Navigation brings these concepts to Jetpack Compose with composable destinations.