android shared preference is null - android

i am using shared preference in android but it return null,
i saw a lot of code example for this and i can't see any error in my code
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Log.v("sharedpref",""+username+": "+password);
editor.putString("email", username);
editor.putString("pass",password);
editor.apply();
and here i am retrieving the data from shared preference (in another activity)
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Log.v("shersend",""+sp.getString("email", "Empty")+": "+sp.getString("pass", "Empty"));
String email = sp.getString("email", "Empty");
String pass = sp.getString("pass", "Empty");
so is there any problem with my code?
and is there a better to write this?

You are saving the preference in one Activity say A, And accessing in another activity say B, and context of both activities are different and so preferences values cannot be accessed as mode is private.
try
this.getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

Please try that way, Change this code block
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
to this code block
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Use getApplicationContext()
SharedPreferences sp =getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Log.v("sharedpref",""+username+": "+password);
editor.putString("email", username);
editor.putString("pass",password);
editor.commit();

Related

Unable Create any SharedPreferences

I usually use this code for creating sharedPrefs file:
SharedPrefrences SP = getSharedPrefrences("MYPREF",0);
SP.getString("Username","x");
But now if I see this directory:
"/data/data/packageName/"
There is no shared_prefs directory.
Another thing : if I use Editor its work properly.
First you need to store some data in shared preferences using apply()/commit() method so that you retrieve data from there.
SharedPrefrences SP = getSharedPrefrences("MYPREF",0);
SP.edit().putString("Username","SOME NAME").apply();
System.out.println("Username is : "+SP.getString("Username","x"));
First of all you have to SET the data with an Editor
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Pepito");
editor.apply();
Then you can read it as follows :
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "Here's the default value");
If you've set the value after make that getString() your String value will be "Pepito", otherwise you'll get "Here's the default value"

Share preferences not accesible via IntentService

I am saving a value in Preference, but it always give me the default value. When the app is opened, I can get the actual value. But when I am getting the value from the IntentService, it always give me the default value.
Code for saving the value:
prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
Code for reading the value:
prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
String value = prefs.getString(key, defaultValue);
But if i change file name then its working for some period but afterward again it start giving default value
Try changing your access mode from Context.MODE_PRIVATE since you are reading it from outside.
Try
prefs = context.getSharedPreferences(NAME, Context.MODE_MULTI_PROCESS);
Retrieving stuff from Shared Preferences:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);
String s = sp.getString("key", null); // get "value" from the Shared Preferences

SharedPreferences deleted for no apparent reason?

I am using following code:
SharedPreferences sharedPref = getSharedPreferences(GlobalDefines.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putBoolean(GlobalDefines.USER_IS_LOGGED_IN, false);
editor.remove(GlobalDefines.USER_NAME);
editor.remove(GlobalDefines.USER_PASSWORD);
editor.commit();
test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
The string "test" has a value when I get the value from the shared preferences for the first time; when I remove another value from the preferences and want to get the same value (GCM_KEY) again, it is returned empty.
Why is that?
editor.clear() tells the editor that you want to remove ALL values from your SharedPreferences. Remove this line and you will see the expected behavior.

SharedPreferences does not work - getString always returns the default value

I have a problem with SharedPreferences in Android.
This is my code:
SharedPreferences s = this.getSharedPreferences("kurs",MODE_WORLD_READABLE);
s.edit().putString("eur", "1.80");
s.edit().commit();
SharedPreferences a = this.getSharedPreferences("kurs",MODE_WORLD_READABLE);
String kurs = a.getString("eur","7");
Toast hhh= Toast.makeText(getApplicationContext(),kurs, Toast.LENGTH_LONG);
hhh.show();
I´m setting the String and want to read it out directly after that in the onCreate method. But i always get the specified default value "7".
What was wrong? I already researched for that problem, but i can´t found helpful things.
Thanks for your help :)
Each time you call "s.edit()" a new editor is created. Thus your "commit()" call is on an instance of the editor that has not had your setting applied. Try this:
SharedPreferences s = this.getSharedPreferences("kurs",MODE_WORLD_READABLE);
Editor editor = s.edit();
editor.putString("eur", "1.80");
editor.commit();
Please try my code below. What i think is wrong in your code, that you are using different "Editor" instances here:
"s.edit().putString("eur", "1.80");"
and here
s.edit().commit();
private static String APP_SHARED_PREFS = "MyAppID";
// Write the value
SharedPreferences.Editor prefsEditor = getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE).edit();
prefsEditor.putString("KEY", "VALUE");
prefsEditor.commit();
// Get the value
return getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE).getString("KEY", "");
SharedPreferences myPrefs = this.getSharedPreferences("kurs", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("eur", "1.80");
// commit the edits
editor.commit();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", context.MODE_WORLD_READABLE);
String kurs = myPrefs.getString("eur", "7");
Toast hhh= Toast.makeText(getApplicationContext(),kurs, Toast.LENGTH_LONG);
hhh.show();
Try This

How to store variable in phone memory android?

i need to store a variable in phone memory, and later get that variable to show it.
Please, give me some information or anything to search. Thanks
static SharedPreferences settings;
static SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
editor.putString("Variablenname_1", "1");
editor.commit();
if you want so get the value use:
String val = settings.getString("Variablenname_1", "0");
I think Shared Preferences is a good idea for you, read this for more information: http://developer.android.com/guide/topics/data/data-storage.html
The final result.
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String text = app_preferences.getString("name", "default");
...
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("name",name.getText().toString());
editor.commit();

Categories

Resources