Navigation view items will not respond when pressed

Stradtdog :

I am working on an app with a side Navigation drawer. The drawer opens fine, however the text that supposedly can be "clickable" does not seem to respond. The animation shows that there is feedback to when the drawer is tapped (you can hear the sound) however nothing results of it. I have tried to place toast messages to see if the button registers an action, but when pressed, no toast appears. The code goes as follows (I have implemented NavigationView.OnNavigationItemSelectedListener):

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        // 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_history, R.id.nav_settings,
                R.id.nav_help, R.id.nav_signout)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

And then I implemented the method:

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_history:
    Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
                break;
            case R.id.nav_help:

                break;
            case R.id.nav_settings:

                break;
            case R.id.nav_signout:
                signOut();
                break;
        }

        DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Thank you

ianhanniballake :

The line

NavigationUI.setupWithNavController(navigationView, navController);

Calls setNavigationItemSelectedListener internally to connect destinations to menu items (i.e., when you click on the R.id.nav_settings MenuItem, it'll replace the Fragment in your NavHostFragment with the one with android:id="@+id/nav_settings" set). This listener overrides the OnNavigationItemSelectedListener view you've set, which is why your custom logic doesn't run.

If you want to combine both sets of functionality together, you need to call navigationView.setNavigationItemSelectedListener(this); after setupWithNavController and trigger the default behavior with NavigationUI.onNavDestinationSelected():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver_home);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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_history, R.id.nav_settings,
            R.id.nav_help, R.id.nav_signout)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
    // This line needs to be after setupWithNavController()
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    switch (menuItem.getItemId()){
        case R.id.nav_history:
            Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
            break;
        case R.id.nav_signout:
            signOut();
            break;
        default:
            // Trigger the default action of replacing the current
            // screen with the one matching the MenuItem's ID
            NavigationUI.onNavDestinationSelected(menuItem, navController);
    }

    DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357482&siteId=1