Android SharedPreferences empty after commit - android

Why is in the following example the foo false?
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true);
mPrefs.edit().commit();
boolean foo = mPrefs.getBoolean("myValue",false);

I think you have to write in this way
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true).commit();
boolean foo = mPrefs.getBoolean("myValue",false);

When you call edit() the first time, you are not saving the boolean and so it does not exist when you retrieve it.
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true).commit();
boolean foo = mPrefs.getBoolean("myValue",false);

Store your values by--->
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("myValue",true);
editor.apply();
to get your result--->
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean b = sharedPreferences.getboolean("myValue","");

Save value in shared preferences:
SharedPreferences settings =getSharedPreferences("AppName", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor. putBoolean(key, value);
editor.commit();
get value from shared preferences:
SharedPreferences settings = getSharedPreferences("AppName", 0);
String value=settings.getString(key, "");
boolean value=settings.getBoolean(key,false);

You should Edit the SharedPreference object through Editor
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("Key", value);
editor.commit();

Related

Editing shared preference in other activity

how can I edit value of sharedPreference from other Activity. I try this codes by I'm getting an error on the context part.
if(stars == 2){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = scorepref.edit();
editor.putInt("keyhelloworld", stars);
editor.commit();
Intent fromHW = new Intent(HelloWorldGameActivity.this, LessonActivity.class);
startActivity(fromHW);
}
try this.
Pass context from first activity.
To call Shared preferences
SharedPreferences sharedPreferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
To call editor
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
You can use SharedPreferences as follows. Since sharedpreferences is persistent, you can use the same implementation anywhere in your application to access it.
SharedPreference sharedPreferences = getApplicationContext().getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("key", value).apply();

How to Save and Retrieve Boolean Flags from Shared Preferences

How to Save and Retrieve boolean flags in Shared Preference. I tried below thing but doesn't seem to be working
Save :
SharedPreferences prefs = activity.getSharedPreferences(PREFERENCES_EULA, Activity.MODE_PRIVATE);
Boolean lock = prefs.edit().putBoolean("locked", true).commit();
Retrieve :
SharedPreferences prefes = PreferenceManager.getDefaultSharedPreferences(context);
Boolean yourLocked = prefes.getBoolean("locked", false);
should get "true" here as i am passing "true" while saving
I think you are getting two different instances of SharedPreferences.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("locked", true);
editor.commit();
then
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean value = sharedPref.getBoolean("locked", false);

Cannot get SharedPreferences value from one activity to another in Android

I am using shared preferences for saving data and accessing it from another activity. I have used suggested methods but they don't seem to work.
Code:
private static String Module_Pref="ModulePreference";
Activity A:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
Activity B:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tempValue= sharedPreferences.getString(Module_Pref, "empty");
Activity C:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
Here if we run first time then pass through A, set nosave, then if we go to activity C then save the data.
What is wrong with this code.I am getting a null. I looked at file explorer also where pref file isn't saved.
use this method to create a sharedPreference and then access it with this same name from any where in any activity within the same app
SharedPreference sp;
sp = getApplicationContext().getSharedPreferences(My_PREFERENCE,
context.MODE_PRIVATE);
Editor e = sp.edit();
e.put(key,value);
e.commit();
and when getting the same sharedPreference in another activity use this method
SharedPreference sp;
sp = getApplicationContext().getSharedPreferences(My_PREFERENCE,
context.MODE_PRIVATE);
sp.get(key,value);
`
Your should use for entering data like this:
editor.putString(Module_Pref, "value that you want to store");
editor.commit();
Now For getting that String you can use this after storing the value:
sharedPreferences.getString(Module_Pref, "empty");
sharedPreferences.getString(Module_Pref, "empty");
means you want to retrieve the "value" for the key- Module_Pref, if there is none, by default it will return -> "empty" .
So either,
editor.putString(Module_Pref, value);
Or
sharedPreferences.getString(key, "empty");
where "key" is same as the key in:
editor.putString(key, value);
Activity A:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key,value );
editor.commit();
Activity B:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tempValue= sharedPreferences.getString(key, "emptyValue");
SharedPreferences sp = getSharedPreferences("main",0);
SharedPreferences.Editor ed = sp.edit();
ed.putString("personEmail",personEmail);
ed.putString("personName",personName);
ed.commit();
this is used for saving values in shared pref & for accessing it use ->
SharedPreferences sp = getSharedPreferences("main",0);
sp.getString("personEmail",null);
sp.getString("personName",null);

How To Use SharedPreference value stored in one activity to another activity

My Code is:
SharedPreferences myPrefs = this.getSharedPreferences("MYPREF'USERID",
Context.MODE_PRIVATE);
int uid = myPrefs.getInt("USERID", 0);
I want to use USERID in another two activities for fetching records
Please help me how to do
Thank You
You can set values in preference as follows
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
editor.putInt ("USERID", 0);
editor.commit();
and you can retrieve the data form preference as
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
final int userId= pref.getInt("USERID", -1);
You can get SharedPreferences all around your application by using:
SharedPreferences sharedPreferences = Context.getApplicationContext().getSharedPreferences("pref", Context.MODE_PRIVATE);
You also want to put your values in this global sharedPreference.

Android Shared Preferences betwen Activities

I've on Activity A the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
I've on Activity B the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a); //TextBox
Can anyone tell why it's not showing (the value saved on Activity A) on Activity B?
Refer the below code
Activity A
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
Activity B
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a);
its better use getter and setter with the help of string.xml file, you will never get this kind of problem. for that matter you may check this blog:
http://sspower3.blogspot.in/2011/11/sharedpreferences-in-eazy-way.html

Categories

Resources