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?

Ling
  • 449
  • 6
  • 21
  • 2
    There's a issue in 3.5 that's causing XML to be rearranged improperly. In the `activity_main` layout, make sure that the `` is listed last within the ``. That is, move it to after the `` there, if you still have the default setup. Then, have a look at [this post](https://stackoverflow.com/q/57591080) to see how to stop Android Studio from reordering your `View`s like that. – Mike M. Sep 13 '19 at 22:19
  • Hi, thank you very much for this hint. I will try this when I’m getting home tomorrow. – Ling Sep 14 '19 at 05:48
  • 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? – Ling Sep 16 '19 at 10:42
  • Sorry, your comment got kinda lost in my inbox, and I didn't see it until now. Thanks for posting a proper answer. Cheers! – Mike M. Sep 19 '19 at 04:08

3 Answers3

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.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
Ling
  • 449
  • 6
  • 21
  • 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 as you suggested, but I can't resolve.. please help – Marco Nov 22 '19 at 11:42
  • You saved my day. Not all hero wears cap. – Samyak Dec 08 '19 at 12:14
  • 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. – Elletlar Dec 01 '20 at 16:06
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.

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

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31583549) – Kai-Sheng Yang Apr 25 '22 at 02:24