简体   繁体   中英

Android Drawer - Navigating to Multiple Fragments using Navigation.findNavController()

I do not know if I am doing this right. I use a Navigation Drawer template in Android Studio and successfully browsing 3 menu option. What I did is for all menu I added if else what the next fragment will be shown. I feel like this kind of approach is kind of hard specially I need to add more menu.

Is their a better way for navigating fragments. Thanks in advance.

MainActivity.java

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    String title =  getSupportActionBar().getTitle().toString();
    switch (item.getItemId()) {
        case R.id.nav_home:
            if(title.equals(getString(R.string.title_status))) {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_status_to_home_fragment);
            } else if(title.equals(getString(R.string.title_gallery))) {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_galley_to_home_fragment);
            }
            break;
        case R.id.nav_status:
            if(title.equals(getString(R.string.title_home))) {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_home_to_status_fragment);
            } else if(title.equals(getString(R.string.title_gallery))) {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_galley_to_status_fragment);
            }
            break;
        case R.id.nav_gallery:
            if(title.equals(getString(R.string.title_home))) {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_home_to_gallery_fragment);
            } else if(title.equals(getString(R.string.title_status))) {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.action_status_to_gallery_fragment);
            }
            break;
    }

    item.setChecked(true);
    drawer.closeDrawers();
    return true;
}

mobile_navigation.xml

<?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/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.ddc.qvac.framents.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_home_to_status_fragment"
            app:destination="@id/nav_status" />
        <action
            android:id="@+id/action_home_to_gallery_fragment"
            app:destination="@id/nav_gallery" />
    </fragment>

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.ddc.qvac.framents.gallery.GalleryFragment"
        android:label="@string/title_gallery"
        tools:layout="@layout/fragment_gallery">
        <action
            android:id="@+id/action_galley_to_home_fragment"
            app:destination="@id/nav_home" />
        <action
            android:id="@+id/action_galley_to_status_fragment"
            app:destination="@id/nav_status" />
    </fragment>

    <fragment
        android:id="@+id/nav_status"
        android:name="com.ddc.qvac.framents.StatusFragment"
        android:label="@string/title_status"
        tools:layout="@layout/fragment_status">
        <action
            android:id="@+id/action_status_to_home_fragment"
            app:destination="@id/nav_home" />
        <action
            android:id="@+id/action_status_to_gallery_fragment"
            app:destination="@id/nav_gallery" />
    </fragment>
</navigation>

use same id for:

  • id that defined in fragment tag in navigation xml file
  • id that defined in menu xml file

in your mobile_navigation.xml file you are using "@+id/nav_home" for home fragment. in your menu.xml file make id of home menu as "@+id/nav_home"

if both ids are same in menu file and navigation file, it will automatically navigate to the fragment. we don't need to do any boilerplate code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM