I have this code:
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, 0);
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
if (!((LoginButton.email).equals(""))) {
//settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
// Commit the edits!
editor.commit();
Log.d("hasLoggedIn in email check = ", hasLoggedIn + "");
}
}
even after entering the if, the last Log gives me hadLoggedIn as false.
Somewhere in the same activity i got the same code of editing which works fine, but the only difference is i never used it as soon as it got edited, i used it when the activity is called again.
You will again have to open sharedpreference for reading.
your hasLoggedIn still contains old value.
I have updated your code as below.