1

when click on any menu item, there is no response, they suppose to navigate to Destination Fragment

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        item.setChecked(true);

        int id = item.getItemId();
        switch (id) {

            case R.id.mealFragment:
                navController.navigate(R.id.mealFragment);
                break;

            case R.id.drinksFragment:
                navController.navigate(R.id.drinksFragment);
                break;

            case R.id.dessertFragment:
                navController.navigate(R.id.dessertFragment);
                break;

        }
        drawer.closeDrawer(GravityCompat.START);
        return true;

    }






// setting up on time Navigation 

private void setUpNavigation() {
    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, drawer);
    NavigationUI.setupWithNavController(navigationView, navController);
    navigationView.setNavigationItemSelectedListener(this);

}

after call setUpNavigation() . App keeps sticking on mealFragment() as its start Destination on navigation graph

robo
  • 115
  • 10
  • 1
    If `bringToFront()` fixed your issue, then the ``s inside your `` in the layout XML are in the wrong order. The drawer `` must be listed last in order to end up on top, and receive touch events properly. If you did not move the ``s like that yourself, then it's possibly due to an issue in the 3.5 upgrade for Android Studio that causes XML to be rearranged improperly. If that's your case, have a look at [this post](https://stackoverflow.com/q/57591080) to see how to fix it. – Mike M. Sep 05 '19 at 23:35
  • 1
    Also, setting your own `OnNavigationItemSelectedListener` like that is overriding the `NavigationUI.setupWithNavController()` call, essentially defeating the purpose of the Navigation framework there, which should be handling the `Fragment` navigation for you automatically. If you did that in an attempt to fix the drawer issue, you can remove that after fixing your layout, and the IDE issue. – Mike M. Sep 05 '19 at 23:35
  • yes, the problem came after 3.5 android studio update, overriding the framework method just to check where the error and why navigation isn't work – robo Sep 06 '19 at 00:16
  • 1
    Yeah, it's a common issue. You should just have to rearrange your layout XML, and then you can go back to using the Navigation framework. – Mike M. Sep 06 '19 at 00:18

3 Answers3

1

I was programmatically adding HeaderView to NavigationView Hence I already had NavigationView

I called navigationView.bringToFront();

Here is code snippet for context :

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.bringToFront();
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
0

Did you set listner for navigationView as:

 NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
 navigationView.setNavigationItemSelectedListener(this);

befor implementing the onNavigationItemSelected(MenuItem item) as:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camara) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
Manoj Kumar
  • 332
  • 1
  • 7
0

Check with a Log.d() call if the onNavigationItemSelected event is fired or not. If not, check your the layout file for the clickable attribute:

<DrawerLayout
    android:clickable="true"
    android:focusable="true"
Tomee
  • 326
  • 1
  • 5