I am following this answer on SO, which is titled as:
Change NavigationView items when user is logged
The code works fine but the content of NavigationView
change when I restart the app. I want the content to be changed after I click Login or LogOut item menus
This is my code in onCreate()
method:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer1);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
and here is the islogin()
method:
public boolean islogin(){
// Retrieve data from preference:
prefs = getSharedPreferences("UserLoginData", MODE_PRIVATE);
String username = prefs.getString("username", null);
if (username == null) {
return false;
}
else{
return true;
}
}
Any help would greatly appreciated! Thank You
Note: Though this question seems duplicate of some, but its just the title, contents are entirely different.