SharedPreferences nie działa jak powinno

0

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

2

Po edycji danych wywołaj .commit() lub .apply() - w zależności od asynchroniczoności.

1 użytkowników online, w tym zalogowanych: 0, gości: 1