I found an easier way to change the navigation view (working with submenus and menus). You can re-inflate the NavigationView at run time with two lines of code. In this example, I re-inflate with new_navigation_drawer_items.xml when the user successfully logged in
navigationView.getMenu().clear(); //clear old inflated items. navigationView.inflateMenu(R.menu.logged_in_navigation_drawer_items); //inflate new items.
When the user logs out, inflate it again with logged_out_navigation_drawer_items.xml
navigationView.getMenu().clear(); //clear old inflated items. navigationView.inflateMenu(R.menu.logged_out_navigation_drawer_items); //inflate new items.
Thus, he actually inflates the elements, but does not add new objects to the existing one. Just create your own menu.xml
source share