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.
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();
}
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();
Related
I want to make a activity in an app which shows up only when the user installs the app and never again. How can I do that?
1.store value in sharedPrefernces.
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("screen_show", false);
editor.commit();
2. get value from sharedPreferences
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
preferences.getBoolean("screen_show", false);
3.It is false first time always
if( ! preferences.getBoolean("screen_show", false)){
// if show screen
Intent showscreenIntent=new(this,ShowScreen_Intent.class);
startActivity(showscreenIntent);
} else {
//
}
4.after showing screen first time set true in shared prefernece like this.
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("screen_show", true);
editor.commit();
Now whenever step 3 execute else condition run and Activity will never show again.
hope it helps !!!
My issue solved by trying out this
boolean isFirstRun = getSharedPreferences("Preference", MODE_PRIVATE).getBoolean("isfirstrun",true);
if(isFirstRun){
getSharedPreferences("Preference", MODE_PRIVATE).edit().putBoolean("isfirstrun",false).commit();
Intent IntentFirstRunAct = new Intent(MainActivity.this,FirstRunActivity.class);
startActivity(IntentFirstRunAct);
}
I'm trying to use sharedpreferences to store an integer. Something is wrong though, so the integer won't get saved. Whenever I stop my app the integer won't show up, it isn't there and isn't getting saved. Here's my code that's located in my onCreate() method:
private int point;
//...
SharedPreferences sp = getSharedPreferences("prefs_file",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", point);
editor.apply();
SharedPreferences sp1 = getSharedPreferences("prefs_file",MODE_PRIVATE);
int highScoreSaved = sp1.getInt("your_int_key", 0);
Try sp1.commit().
Have a look at the developer guide:
https://developer.android.com/guide/topics/data/data-storage.html#pref
I'am trying to put a shared preference in a bundle so i can use it in another class.
So for example I have a class that views the strings inside a shared preference.
Then i have another class which can edit the string.
In my main class where i have created the bundle:
SharedPreferences sharedpreferences;
Intent i = new Intent(getBaseContext(),verification.class);
i.putExtra("sharedpreferences", sharedpreferences);
The issue is with the putExtra. it works for normal strings but not for the bundles, any ideas, i think its something simple
Of course Intent.putExtra(...) works with Bundles:
Intent.putExtra(String name, Bundle value);
Anyways, there is no need to pass the SharedPreferences as a Bundle to the next Activity.
Simply retrieve the SharedPreferences from the next Activity itself.
Save stuff into the SharedPreferences:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
Editor e = sp.edit();
e.putString("key", "value"); // save "value" to the SharedPreferences
e.commit();
Retrieve stuff from the SharedPreferences:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
String s = sp.getString("key", null); // get "value" from the SharedPreferences
This makes not that much sense, but here is how to put a String from the SharedPreferences into an Intent:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
Intent i = new Intent(Context, YourActivity.class);
i.putExtra("key", sp.getString("key", null));
I have developed an App where the user has to login into the app using the login Activity. After successful login, the app is directed to Activity B. The app doesn't provide any signout options.So next time when the user uses the app I want to hide the login Activity's layout. Is it possible to do so?If yes, how is it possible?
In LoginActivity do as Suggested by nr4bt but instead of int Put & get Boolean Value in SharedPreference.
write this code
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putValue("loginStatus", true);
editor.commit();
& onCreate() of LoginActivity Write this code
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getBoolean("loginStatus", false);
if (restoredText == true) {
// start activity
finish(); // destroy login so user can't come back with back button
}
Hope this helps you.
Keep a variable in sharedpreferences and check it when your login activity starts
in LoginActivity, when the user press login button and if success,
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("loginStatus", 1);// removed ""
editor.commit();
Then in onCreate check the variable,
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int restoredText = prefs.getInt("loginStatus", 0);
if (restoredText == 1) {
// start activity
finish(); // destroy login so user can't come back with back button
}
It is quite simple to achive with SharedPreferences. You just save that user has loged in and later on start check if he already done it or not and act properly.
I am getting weird problem with my app.
I have set-up one SharedPreference, like this
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
flag = prefs.getBoolean("handle_calls", false);
if (flag) {
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putBoolean("checkFlag", true);
editor.commit();
}
it was working fine some days before, but now this code is working fine but when I try to read these preferences in my SmsReceiver Class SharedPreferences doesnt read these values and default values are read (false)...Sometimes it work and most of the time it doesnt work at all!
Here is how I am reading the SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SmsManager sms = SmsManager.getDefault();
//flag = prefs.getBoolean("handle_calls", false);
checkFlag = prefs.getBoolean("checkFlag", false);
checkDecisionFlag = prefs.getBoolean("checkDecisionFlag", false);
This checkDecisionFlag is working fine but checkFlag is taking default values.
Really pissed! Please Help!
remove the editor.clear(); from if condition. as clear will clear all the data from SharedPreference.
if (flag) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("checkFlag", true);
editor.commit();
}
Solved it!
This code was fine, instead the problem was with the life-cycle oriented. I had one another SharedPreference on which this depend. That was getting false again and again.
I think you wanted to use if (!flag) (not flag) on the third line of the first code sample.