Clicking on item of Navigation Drawer doesn’t open fragments
Asked Answered
V

3

5

I want to use the default Navigation Drawer Activity of Android Studio (v. 3.5). After creating this default activity (New Project --> Navigation Drawer Activity) I started this templet. If I click on one of the icons of the navigation menu (e.g. “Gallery”) the current fragment of the NavHost doesn’t change. As I understand the following section: https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer the NavController should navigate to the chosen destination if the id of the Menu Item matches the id of the destination, but this doesn’t work.

After researching the whole day, I found the following Question on Stackoverflow : NavigationView click event which is very similar but not really answered. The next interesting point is that if I use the Button Navigation Activity this template seems to work probably and I can’t really figure out the differences to the Navigation Drawer Activity templet. I also found several other solutions which worked for older Templets. I can’t understand why there is a standard templet which seems not to work and why there are also no proper examples and tutorials provided via https://developer.android.com/ .

mobile_navigation.xml (navigation graph):

<?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_tools"
        android:name="ktu.workout_planner.ui.tools.ToolsFragment"
        android:label="@string/menu_tools"
        tools:layout="@layout/fragment_tools" />
</navigation>

activity_main_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
...
        <item
            android:id="@+id/nav_tools"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/menu_tools" />
    </group>

As you can see the id of the item matches the id of the fragment.

onCreate function of MainActivity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

Did I miss something why clicking on the item of the Navigation Drawer doesn’t open the corresponding fragments?

Vaduz answered 13/9, 2019 at 20:16 Comment(4)
There's a issue in 3.5 that's causing XML to be rearranged improperly. In the activity_main layout, make sure that the <NavigationView> is listed last within the <DrawerLayout>. That is, move it to after the <include> there, if you still have the default setup. Then, have a look at this post to see how to stop Android Studio from reordering your Views like that.Broncho
Hi, thank you very much for this hint. I will try this when I’m getting home tomorrow.Vaduz
Perfect, thank you very much, now it works! Can you maybe write your comment as an answer, so I can mark it as the solution for my answer?Vaduz
Sorry, your comment got kinda lost in my inbox, and I didn't see it until now. Thanks for posting a proper answer. Cheers!Broncho
V
13

Answer by Mike M. (see comment):

There's an issue in 3.5 that's causing XML to be rearranged improperly. In the activity_main layout, make sure that the <NavigationView> is listed last within the <DrawerLayout>. That is, move it to after the <include> there, if you still have the default setup. Then, have a look at this post to see how to stop Android Studio from reordering your View s like that.

Vaduz answered 18/9, 2019 at 18:34 Comment(3)
I've same problem, I added a new item with relatives files (fragment, viewmodel, item, etc..) but when I click on it, it does not open relative fragment. I compared every single line with other (predefined) items, but nothing wrong. I have <NavigationView> as you suggested, but I can't resolve.. please helpFreytag
You saved my day. Not all hero wears cap.Derbent
Encountered the same issue on Android Studio 4.1.1 with API 30 while doing the Google's Navigation Code Lab. They instruct "Now add the drawer, which is a NavigationView that uses the navdrawer_menu that you just defined. Add the following code in the DrawerLayout, after the [the existing layout]". So they recommend at the bottom too, but without saying why.Torques
C
0

I have same issue. I just put navigationview last in mainActivity.xml file then put include at first within drawerlayout then it working fine.

Canonical answered 26/4, 2021 at 18:27 Comment(0)
P
0

I had the same problem and I looked everywhere but no suggestion seemed to work until when I changed the layout I wrapped in the toolbar and fragment container from to

Pless answered 20/4, 2022 at 18:4 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewEncarnalize

© 2022 - 2024 — McMap. All rights reserved.