SharedPreferences not Reading - java

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.

Related

How to call a method just before an app is closed?

I have a variable who contains data that can change when user is using app.
My goal is to store those data in SharedPrefs just before user is closing app,
I was thinking about something like
OnStop() {
Preferences.edit()... }
but i don't know the correct syntax ...
thanks in advance guys
Save your data in SavedPrefs in onStop Method.
Syntex will be like
In onStop Method
#Override
protected void onStop() {
super.onStop();
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = saved_values.edit();
edit.putString("key", "stringToBeSaved")
editor.commit();
}
And then to retrive your data in onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String foo = saved_values.getString("key", "default_string");
}
Remember the key should be same.
For more info check out Android docs on
Shared Preferences: Save key-value data
Android Lifecycle: Understand the Activity Lifecycle
Activity.onStop() is called just before the app is completely closed (kicked from recents)
You should probably use Activity.onPause() to save your preferences as it will also be called when the user just switches to another app.
Further information:
https://developer.android.com/guide/components/activities/activity-lifecycle
You should save your persistent data in onPause() rather than in onStop().
Then you can retrieve your data in onResume().
For saving the data you can take a look at this example
SharedPreferences.Editor editor = getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("username", "My name");
editor.apply();
And then retrience your data like this
SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
String username = prefs.getString("username", "No username"); //No username is the default value

Make a activity screen which shows up only once

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);
}

SharedPreferences not saving properly - android

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

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.
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();

Android SharedPreferences issue

I need a little help with Android Shared Preferences. I'm trying to put a boolean type in SP and make it visible from every other activity in my application.And I want to be able to change the state of boolean type to true/false from another activity so I can make some changes in the UI depending on that boolean value.
For now I'm using this piece of code,which I understand but it's not correct.
Here it is :
Activity 1:
boolean isLoggedIn = false;
SharedPreferences isLogged = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = isLogged.edit();
editor.putBoolean("isLoggedIn", isLoggedIn);
editor.commit();
Activity 2 :
boolean isLogged=true;
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences;
mySharedPreferences=getSharedPreferences("isLoggedIn",mode);
mySharedPreferences.edit().putBoolean("isLoggedIn", isLogged);
boolean bool = mySharedPreferences.getBoolean("isLoggedIn",false);
Log.w("Boolean","Boolean state : "+bool);
In Activity 2 try using like this and it will work
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
and remove below line
mySharedPreferences.edit().putBoolean("isLoggedIn", isLogged);
Put edit.commit(); after mySharedPreferences.edit().putBoolean("isLoggedIn", isLogged);
This will help you to get correct value.

Categories