Cześć. Mam jakiś dziwny problem z SharedPreferences. Zaimplementowałem to tak jak zawsze, a tym razem nie działa wrzucanie do nich nawet typów podstawowych.
Klasa AppSharedPreferencesHelper
public class AppSharedPreferencesHelper implements SharedPreferencesHelper{
private static final String BOOL_LOGGED_IN_USER = "BOOL_LOGGED_IN_USER";
private static final String USER_LOGGED_IN_USER = "USER_LOGGED_IN_USER";
private final SharedPreferences mPrefs;
private SharedPreferences.Editor editor;
public AppSharedPreferencesHelper(Context context, String prefFileName){
mPrefs = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE);
}
@Override
public boolean isUserLoggedIn() {
return mPrefs.getBoolean(BOOL_LOGGED_IN_USER, false);
}
@Override
public void setUserLoggedIn(boolean isUserLoggedIn) {
mPrefs.edit().putBoolean(BOOL_LOGGED_IN_USER, isUserLoggedIn);
}
Pewna metoda w MainActivity.class
private void enterApplicationIfLoggedIn(){
sharedPrefs = new AppSharedPreferencesHelper(this, "Shared_Preferences");
boolean isUserLoggedIn = sharedPrefs.isUserLoggedIn();
Log.d(TAG, "enterApplicationIfLoggedIn: isUserLoggedIn = " + isUserLoggedIn);
sharedPrefs.setUserLoggedIn(true);
boolean sialala = sharedPrefs.isUserLoggedIn();
Log.d(TAG, "enterApplicationIfLoggedIn: sialala = " + sialala);
if(isUserLoggedIn){
startActivity(new Intent(LauncherActivity.this, MainActivity.class));
}
}
LOGI
2020-11-16 11:52:00.442 13856-13856/borgwarner.com.pickmeup D/myLogs launchAct: enterApplicationIfLoggedIn: isUserLoggedIn = false
2020-11-16 11:52:00.442 13856-13856/borgwarner.com.pickmeup D/myLogs launchAct: enterApplicationIfLoggedIn: sialala = false
Nawet nie wiem jak to debugować. Nie za bardzo rozumiem dlaczego chociaż wyraźnie wrzucam do SharedPreferences true, to nadal wyciągam defaultValue. Ja nie wiem, czy coś się zmieniło w ostatnim Androidzie ? :p