i have an app with login logout session with pref menu. i want to when user is login, option menu only show log out menu. But when user is log out, option menu is only show login menu. i have try some example but in my case always give me error in major.
this is my main.xml
<item
android:id="@+id/Login"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/Login">
</item>
<item
android:id="@+id/Logout"
android:title="Logout" />
this is my config.Java
public class Config {
//URL to our login.php file
public static final String LOGIN_URL = "http://www.kinandayu.com/login.php";
//Keys for email and password as defined in our $_POST['key'] in login.php
public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";
//If server response is equal to this that means login is successful
public static final String LOGIN_SUCCESS = "success";
//Keys for Sharedpreferences
//This would be the name of our shared preferences
public static final String SHARED_PREF_NAME = "myloginapp";
//This would be used to store the email of current logged in user
public static final String EMAIL_SHARED_PREF = "email";
//We will use this to store the boolean in sharedpreference to track user is loggedin or not
public static final String LOGGEDIN_SHARED_PREF = "loggedin";
}
this is my option menu in MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.Login:
Intent a = new Intent(MainActivity.this, LoginActivity.class);
startActivity(a);
return true;
case R.id.Logout:
logout();
return true;
default:
return super.onOptionsItemSelected(item);
}
Can anybody help me in my case, every answer is helpful for me.
Thanks in advance
Create to layouts for your menu:
public boolean onCreateOptionsMenu(Menu menu) {
if(LOGGED_IN){
getMenuInflater().inflate(R.menu.logout, menu);
}else{
getMenuInflater().inflate(R.menu.login, menu);
}
return true;
}
Just check the state of the log-in session before inflating the menu