title: How to programmaticallly remove submenu from navigation drawer in Android?
body: <p>I am developing an Android app. Firstly, let me tell you that I am not professional. What I am doing now is I am adding submenu to menu depending on a condition. But I need to do it very often in my app. But my problem is I added a submenu to the menu as first time.</p>
  
  <p>But second time when I update menu depending on condition, existing submenu is not removed and new submenu is appended to navigation drawer. How can I remove submenu that is programmatically added to menu? Why my code is not removing it?</p>
  
  <p>Here is my code</p>
  
  <pre><code>public void updateAuthUI()
      {
          isLoggedIn = tempStorage.getBoolean(getResources().getString(R.string.pref_is_logged_in),false);
          Menu menu = leftDrawer.getMenu();
          menu.removeItem(getResources().getInteger(R.integer.logout_item_id));
          menu.removeItem(getResources().getInteger(R.integer.login_item_id));
          menu.removeItem(getResources().getInteger(R.integer.register_item_id));
          SubMenu authSubMenu = menu.addSubMenu("Auth");
  
          if(isLoggedIn)
          {
              authSubMenu.add(1,getResources().getInteger(R.integer.logout_item_id),99,"Sign out");
          }
          else{
              authSubMenu.add(1,getResources().getInteger(R.integer.register_item_id),97,"Register");
              authSubMenu.add(1,getResources().getInteger(R.integer.login_item_id),98,"Sign in").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                  @Override
                  public boolean onMenuItemClick(MenuItem item) {
                      openLoginActivity();
                      item.setChecked(true);
                      return true;
                  }
              });
          }
      }
  </code></pre>
  
  <p>Here is the screenshot of my problem</p>
  
  <p><a href="https://i.stack.imgur.com/joAQS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/joAQS.png" alt="enter image description here"></a></p>
  
  <p>As you can see Auth submenu is appended without removing existing one.</p>