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.ktIt would also create respective resource files
Step 2. Add navigation Graph.
Right click on
resfolder.Add
navigation/navigation.xmlfile.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
resfolder.Add a menu
menu/main_overflow_menu.xmlfile.Add one menu item
AboutFile should look as follows.
Notice that menu-item-id is matching to
navigation/navigation.xmlfragment id for about fragment.
Step 4. update MainFragment.kt as follows.
Step 5. Provide Back / Up button support
Open
MainActivity.ktfile.
Last updated
Was this helpful?