简体   繁体   中英

Android Java sharedPreferences logic issue

Android Java sharedPreferences logic issue

I have an app that has a sign in and sign out in it.

I want, when the user first downloads the app, it is sign out. Then when he signs in using the button, a sharedPreference value saves. Now next time he opens the app, it will automatically sign him in. Lets say he signs out then closes the app, so now when he opens it, it will sign him out. Thats how it goes like most apps.

What I did, at the first of the activity I added this

    SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = SiggnedIn.edit();
    editor.putBoolean("SiggnedIn?", false);
    editor.commit();

then when the user clicks the button to signs out

                            SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
                            SharedPreferences.Editor editor = SiggnedIn.edit();
                            editor.putBoolean("SiggnedIn?", false);
                            editor.commit();

and if he clicks it to sign in

                                 SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
                                 SharedPreferences.Editor editor = SiggnedIn.edit();
                                 editor.putBoolean("SiggnedIn?", true);
                                 editor.commit();

and at last, if I have a method that updates everything every second.

so in it, I add

            SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
            boolean myIntValue = SiggnedIn.getBoolean("SiggnedIn?", false);

            if(myIntValue){
                SignHimIn();
            }

That doesn't work.

This is not supposed to work, because each time the app is launched, you are overwriting SiggnedIn? to false.

SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = SiggnedIn.edit();
editor.putBoolean("SiggnedIn?", false);
editor.commit();

I would suggest using another key for SharedPreferences to identify if the app is launched for the first time.

SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);

if (SiggnedIn.getBoolean("APP_LAUNCHED_FIRST_TIME", true)) {
    SharedPreferences.Editor editor = SiggnedIn.edit();
    editor.putBoolean("APP_LAUNCHED_FIRST_TIME", false);
    editor.commit();

    //For the first time, user should be signed out
    editor.putBoolean("SiggnedIn?", false);
    editor.commit();
}

Then in the Activity launch, check if user is signed in or not.

if (SiggnedIn.getBoolean("SiggnedIn?", false)) {
    SignHimIn();
}

Rest Sign in and Sign out logic is Ok. I assume that you are doing this in Button click.

Why are you setting the boolean to false when you first launch your activity? You should just rely on the default value of getBoolean (which you defined as false) - This means that if you didn't put anything yet in the SignedIn field it will still return false.

Solution:

Inside your login button

SharedPreferences mPrefs= getSharedPreferences("mPrefsName", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("isSignedIn",true);
editor.commit();

When your activity starts (notice the false next to isSignedIn - this is the default value)

SharedPreferences mPrefs= getSharedPreferences("mPrefsName", Activity.MODE_PRIVATE);
if(mPrefs.getBoolean("isSignedIn",false))
DoStuff();

And inside logout button (NOT on activity start)

SharedPreferences mPrefs= getSharedPreferences("mPrefsName", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("isSignedIn",false);
editor.commit();

Keep things simple

Check Status

SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
boolean isLoggedIn = pref.getBoolean("isLoggedIn", false);

Sign In

SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLoggedIn", true);
editor.commit();

Sign Out

SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLoggedIn", false);
editor.commit();

Simple Logic

protected boolean isUserSignedIn(){
    boolean isLoggedIn = false;

    SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
    isLoggedIn = pref.getBoolean("isLoggedIn", false);//false is just default

    return isLoggedIn;
}

Usage

if(!isUserSignedIn()){
   SignHimIn();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM