6

I tried by creating a new project with Navigation drawer activity. Got autogenerated fragments with viewmodels. On clicking on the nav menu, it's not navigating to respective fragments.

build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

MainActivity.class

@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, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

@Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

I verified the IDs in navigation graph with Navigation Menu, they are the same. I'm new to Navigation graphs. Any help would be much appreciated.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Shahal
  • 1,008
  • 1
  • 11
  • 29
  • Three years later, and Google still has not fixed this project template. Attempting to debug it shows just how overly complicated (and fragile) they made this navigation system. – Jeremy Frank Oct 10 '22 at 19:58

1 Answers1

12

In your activity_main layout use

<androidx.drawerlayout.widget.DrawerLayout>

  <include/>

  <com.google.android.material.navigation.NavigationView/>

</androidx.drawerlayout.widget.DrawerLayout>

instead of

<androidx.drawerlayout.widget.DrawerLayout>

     <com.google.android.material.navigation.NavigationView/>

     <include/>

</androidx.drawerlayout.widget.DrawerLayout>

The reason is in the doc:

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity>. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 2
    Thanks a lot man. This works. But it doesn't make any sense :/ – Shahal Oct 15 '19 at 06:52
  • 1
    I agree with you @shahal. It worked for me after days trying to make my NavigationView work with NavigationUI. But this it's not mentioned anywhere, and also it's not documented. Jeez, someone can explain why this? Anyway thank you so much Gabriele. – Filipe Bezerra de Sousa Jun 01 '20 at 21:13
  • @FilipeBezerradeSousa I recently had a similar problem. Apparently, it is due to the reverse Z ordering in the XML. So if you define your DrawerLayout first, it actually is second in the Z order. It doesn't make much sense to me either, but I figured I might as well point out the term that guided me to the solution in case anyone else stumbles across this thread. – Dillon Clapp Aug 27 '20 at 02:24
  • Thanks a lotttt. I created a new App, with Drawer, on Android Studio 4.1.2. With no a single change I was facing this issue. – Carlos Iglesias Jul 10 '21 at 04:47