skip to Main Content

Hi guys I have a navigation drawer with several menu items. I am currently implementing a logout feature and I added a logout menu item in the drawer. I currently already have a drawer configuration which opens the previously configured menu items and their corresponding fragments using this configuration.

   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_my_products, R.id.nav_profile, R.id.nav_offers,
            R.id.nav_my_cart,  R.id.nav_my_orders, R.id.nav_log_out)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, 
    R.id.nav_host_fragment_content_main);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);


    I already have the nav controller doing the work for me on opening up the fragments 
     on drawer menu click but the problem for me is that if I click "Log out" in the menu this 
   default to looking for the Log Out fragment to open the corresponding layout. SO I searched for some ways to override this behavior and below seems to be working fine but the problem is it would override selecting each menu items and you would need a very long switch case to implement 
  the override to each which is bit of a hustle not to mention the unnecessary code required 
   with the switch case having to do it for all the menu items when the controller above can 
already do the work of opening the fragments. 

Is there a way to do the override to only 1 menu item selected that does the logout only and preserve the previous controller code that automatically opens up fragment.

  navigationView.setNavigationItemSelectedListener(new 
    NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.nav_home:
                //also if I use this switch case instead how do I open a drawer not an activity that opens on menu click
                   
                    return true;
            }               switch (item.getItemId()){
                case R.id.nav_log_out:
                    logOut();
                    return true;
            }               switch (item.getItemId()){  so on for eahc menu item....



  public void logOut() {
    mFirebaseAuth.getInstance().signOut();
    startActivity(new Intent(this, LoginActivity.class));
    finish();
 }


 

2

Answers


  1. Just override the onOptionsItemSelected to handle the click on the item menu:

           override fun onOptionsItemSelected(item: MenuItem): Boolean {
                when(item.itemId) {
                    R.id.action_logout -> //....) return true
                }
        
                return super.onOptionsItemSelected(item)
            }
    
    Login or Signup to reply.
  2. navView.menu.findItem(R.id.nav_delete_account).setOnMenuItemClickListener   {
            Toast.makeText(this, "Delete account", Toast.LENGTH_SHORT).show()
            true
        }
    

    This worked for me you can override specific menu item behavior by accessing the click listener.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search