Editing shared preference in other activity - android

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

Related

Android SharedPreferences empty after commit

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

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

Shared Preferences in not being cleared,

I'm unable to delete SharedPreferences from the app on click event.
Here is how I'm storing the value into UserInfoActivity SharedPreferences:
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
And here is how I'm trying to clear all data in SharedPreferences from MainActivity:
SharedPreferences clearNotificationSP = getSharedPreferences(
"notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
Please tell what am I doing wrong with this.
Any kind of help will be appreciated.
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();
notificationEditor.remove("notificationCount");
notificationEditor.commit();
or
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();
editor.remove("notificationCount");
editor.commit();
First solution uses the default application preferences file, and the second a custom notification_prefs file.
You are using PreferenceManager.getDefaultSharedPreferences to store but retrieving from getSharedPreferences("notificationCountSP"). They are different files unless you set the default one to "notificationCountSP".
You can do it like below
SharedPreferences userPref = getSharedPreferences(
MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);

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