简体   繁体   中英

onNavigationItemSelected is not working on Navigation Drawer

I'm trying to get click event of Navigation Drawer item. What I did is as below.

(1) Create a new Android source project selecting 'Navigation Drawer Activity' on 'Select a Project Template'. (Min SDK : API 22 - Lollipop)

(2) Add one item on '/res/menu/activity_main_drawer.xml'.

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_menu_camera"
        android:title="@string/menu_home" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="@string/menu_gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="@string/menu_slideshow" />
</group>

<item
    android:id="@+id/menu_notification"
    android:icon="@drawable/ic_menu_slideshow"
    android:title="Notification" />

(3) MainActivity implements NavigationItemSelectedListener, and define event function - onNavigationItemSelected()

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menu_notification) {
        Toast.makeText(this, "Menu - Notification", Toast.LENGTH_SHORT).show();
    }
    return true;
}

(4) Set NavigationItemSelectedListener to MainActivity in onCreate() method.

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);
    navigationView.bringToFront();
    navigationView.setNavigationItemSelectedListener(this);
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this,
        R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

This is all I did. After run this application on Emulator, I clicked new menu item (Notification), but nothing happened. onNavigationItemSelected() can not get event of item click.

What I have to do to get click event of Navigation Drawer item?

The reason why this doesn't work is because you are using Navigation Component . If you want onNavigationItemSelected method to be called, remove the mentioned line below:

    NavigationUI.setupWithNavController(navigationView, navController);

After that, keep in mind that you should manage selected items manually as Navigation won't do it for you anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM