2020-07-17-Android-NavigationComponent-SafeArgs

build.gradle (Project leve)

dependencies {
    ...
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0"
    ...
}

build.gradle (Module: app)

    # MAKE SURE YOU DON'T MISS THIS
    apply plugin: "androidx.navigation.safeargs.kotlin"
    # AND THIS
    dependencies {
        ...
        implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
        implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
        ...
    }
    # AND THIS
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    # AND THIS
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

Step 1. Add fragments

  • Right click on package.

  • Add new fragment.

  • Blank fragment

  • Add MainFragment.kt & DetailFragment.kt

  • It would also create respective resource files

Step 2. Add navigation Graph.

  • Right click on res folder.

  • Add navigation/navigation.xml file.

  • Add both fragments created.

  • After adding those, it should look as follows.

Step 2. Update activity_main.xml

Step 3. Update fragment_main.xml

Step 3. update MainFragment.kt as follows.

Step 4. Update fragment_detail.xml

Step 5. Update DetailFragment.kt

Last updated

Was this helpful?