SharedPreferences in Android - android

I have used SharedPreferences to save some details in the Main Activity .Now i want to get the details in some other activity but i am not able to get them .
Code to save
public void saveInformation(String username,String password) {
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
}
Code to get
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String username = prefs.getString("username", null);
But this is not working for me . How could I get his?

May this help you:
Buddy Change these lines in your code to get:
SharedPreferences prefs = getSharedPreferences("shared", MODE_PRIVATE);
String username = prefs.getString("username","");

Code to get should be
SharedPreferences prefs = getSharedPreferences("shared", MODE_PRIVATE);

Related

Remove Shared preferences key/value pairs

I store some payment values in one Activity
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
productId = spreferences.getString("productId", "");
purchaseToken = spreferences.getString("purchaseToken", "");
orderId = spreferences.getString("orderId", "");
Now I retrieve them in another one as
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
productId = spreferences.getString("productId", "");
purchaseToken = spreferences.getString("purchaseToken", "");
orderId = spreferences.getString("orderId", "");
My question is to delete them in the second Activity after retrieving them.Thanks.
Use SharedPreferences.Editor remove (String key) to do the same.
where it marks in the editor that a preference value should be
removed, which will be done in the actual preferences once commit() is
called.
Note that when committing back to the preferences, all removals are
done first, regardless of whether you called remove before or after
put methods on this editor.
So in your case you can use it like
SharedPreferences.Editor editor = spreferences.edit();
editor.remove("productId");
editor.remove("purchaseToken");
editor.remove("orderId");
editor.commit();
To store values in SharedPreference, use below code:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.putString("productId", "value of prodId");
spreferencesEditor.putString("purchaseToken", "value of purchaseToken");
spreferencesEditor.putString("orderId", "value of orderId");
spreferencesEditor.commit();
To remove specific value from SharedPreference, use below code:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.remove("productId"); //we are removing prodId by key
spreferencesEditor.commit();
To remove All values from SharedPreference, use below code:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.clear();
spreferencesEditor.commit();
To clear the SharedPreferences, use the SharedPreferences Editor
In your case:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = spreferences.edit();
editor.clear();
editor.commit();
You can remove any values associated with a specific key using this,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.remove("your_key");
editor.commit();
or
SharedPreferences prefs = context.getSharedPreferences(name, mode);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(your_key)
editor.commit();
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=spreferences.edit();
editor.remove("productId");
editor.remove("purchaseToken");
editor.remove("orderId");
editor.commit();
// you can also use editor.apply(); instead of editor.commit(); using apply will handle the removing in the background
You need to do same like I am removing my preferences.
SharedPreferences preferences = contextAct.getSharedPreferences("PREF_KEY", 0);
preferences.edit().remove("productId").commit();
preferences.edit().remove("purchaseToken").commit();
preferences.edit().remove("orderId").commit();
Format : preferences.edit().remove("Your Key").commit();
This will clear your preferences.

Android 4.2 SharedPreferences returns wrong value

I'm trying to get some values from SharedPreferences, and I write it in this code;
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(MainActivity.OBSDONE, observationer);
editor.putInt(MainActivity.COROBS, korrekte);
editor.commit();
I got the data from the SharedPreferences-file by pulling it off the Virtual Device, and the data looks correct.
When I try to pull it out from SharedPreferences with;
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int obs = prefs.getInt(OBSDONE,0);
int cor = prefs.getInt(COROBS,0);
it returns 0 to both values?
Use this
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
prefs.edit().putInt(MainActivity.OBSDONE,observationer).apply();
prefs.edit().putInt(MainActivity.COROBS, korrekte).apply();
This worked for me:
pref = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Or if u only have a context:
pref = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

Android Shared Preferences Won't Save

I'm trying out shared preferences, but I can't get my changes to save persistently.
SharedPreferences prefs;
SharedPreferences.Editor prefsEditor;
String lastPlayerPref = "LAST_PLAYER";
public void onCreate(Bundle savedInstanceState) {
prefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
prefsEditor = prefs.edit();
String lastPlayer = prefs.getString(lastPlayerPref, "test");
System.err.println(lastPlayer); //always outputs "test" no matter what I do
prefsEditor.putString(lastPlayerPref, "me");
prefsEditor.commit();
...
}
When I run this initially, I would expect the output "test". When I run it after that, I expect it to output "me". But it always outputs "test". Isn't that argument just a default in the event that no preference has been saved?
Sorry for the confusion. Thanks!
I think you forgot to add .putString(lastPlayerPref, "player1");
prefs = ProgressBarActivity.this.getSharedPreferences("myPrefs", MODE_PRIVATE);
prefsEditor = prefs.edit().putString(lastPlayerPref, "player1");
prefsEditor.commit();
String lastPlayer = prefs.getString(lastPlayerPref, "test");
System.err.println(lastPlayer);
prefsEditor = prefs.edit().putString(lastPlayerPref, "player2");
prefsEditor.commit();
lastPlayer = prefs.getString(lastPlayerPref, "test");
System.err.println(lastPlayer);
if you try above you will see it will change

Saving and retrieving values with SharedPreferences

I want to save two values using shared preferences and get those values in other classes. Can any one please give me information about how to set shared preferences and getting value from shared preferences.
I am using following code:
SharedPreferences settings =
getSharedPreferences("MyGamePreferences", MODE_WORLD_READABLE);
SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences", MODE_WORLD_READABLE);
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("KEY", "e6c77c29021c9b3bd55aa0e9b7687ad9");
prefEditor.putString("SECRET", "ca85fa3fe86edaf2");
prefEditor.commit();
Try this,
SharedPreferences button1;
String name1="",name2="";
button1=this.getSharedPreferences("MyGamePreferences",MODE_WORLD_WRITEABLE);
name1=button1.getString("KEY", "");
name2=button1.getString("SECRET", "");
SharedPreferences.Editor prefEditor = button1.edit();
prefEditor.putString("KEY","e6c77c29021c9b3bd55aa0e9b7687ad9");
prefEditor.putString("SECRET", "ca85fa3fe86edaf2");
prefEditor.commit();
now stored two values.
SharedPreferences myPrefs = this.getSharedPreferences("prefEditor ", MODE_WORLD_READABLE);
String key = myPrefs.getString(KEY, "nothing");
String secret = myPrefs.getString(SECRET, "nothing");
you can retrieve the values, using the getString method by passing the Key and a default value.
http://developer.android.com/reference/android/content/SharedPreferences.html
my problem was how to retrieve these stored values in another file. it was cleared
my code is
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String key = sharedPreferences.getString("key", "");
String secret = sharedPreferences.getString("secret", "");
Thanks .

Android: string value is not getting in Shared Preference

I have created a shared preference for a boolean value and for a string value. The boolean value is gotten in another activity. But for the string I am only getting default value.
Home.class
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor spe = prefs.edit();
spe.putBoolean("flag", true);
spe.putString("user", "hello");
spe.commit();
welcome.class
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean flag= prefs.getBoolean("flag", false);
String user=prefs.getString("user","Nothing");
TextView tv = new TextView(this);
tv.setText("Flag : "+flag+(" User : "+user);
For 'user', only 'Nothing' is displaying. Where should I correct my code?
Try using:
SharedPreferences settings = getSharedPreferences(appName,0);
settings.getBoolean("flag", true);
settings.getString("user", "hello");
And to put:
SharedPreferences settings = getSharedPreferences(appName,0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("flag",true);
editor.putString("user","hello");
editor.commit();
This is what I use in my application, and it shares booleans/ints/strings accrossed many many Classes
Note: appName doesn't have to be the app name, like in the official tutorial.

Categories

Resources