2020-07-16-Android-NavigationComponent

build.gradle (Module: app)

dependencies {
    ...
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
    ...
}

Step 1. Add fragments

  • Right click on package.

  • Add new fragment.

  • Blank fragment

  • Add MainFragment.kt & AboutFragment.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. Add menu/main_overflow_menu.xml file.

  • Right click on res folder.

  • Add a menu menu/main_overflow_menu.xml file.

  • Add one menu item About

  • File should look as follows.

  • Notice that menu-item-id is matching to navigation/navigation.xml fragment id for about fragment.

Step 4. update MainFragment.kt as follows.

Step 5. Provide Back / Up button support

  • Open MainActivity.kt file.

Last updated

Was this helpful?