This question already has an answer here:
- Having trouble skipping an activity 2 answers
I'm developing an app in which a user logs in to his dashboard and stays logged in until he logs out (I mean if the app is closed and restarted, he still stays logged in). I'm using SharedPreferences
to check if the user is actually logged in.
Preference.java
public class Preference {
Context context;
SharedPreferences sharedPref;
public Preference(Context context){
this.context = context;
sharedPref = context.getSharedPreferences("LoginState", 0);
}
public boolean getIsLoggedIn(){
return sharedPref.getBoolean("State", false);
}
public void setIsLoggedIn(boolean state){
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("State", state);
editor.commit();
}
}
I call the function setIsLoggedIn(true)
when the user logs in and setIsLoggedIn(false)
when the user logs out. But how to check and skip the LoginActivity if the user is already logged in?