I am using shared preferences to save 3 string fields. I am opening shared preferences everytime with following code in another class. I am calling this class via a function to save it.
mPrefs=context.getSharedPreferences(id, Context.MODE_PRIVATE);
After doing this, I am saving 3 fields which are id, name and surname of a person. Save process is successful. I am holding all of those 3 variables in an object which is called person. I can get info from shared memory. When I get the info for the first person with his Id, I can do it. I am writing it out to Log. it is successful. But after getting second person info with second person’s id, first person object is also second person. Shared preferences assigns all the variables to same value.
Any helps will be appreciated. Thanks
EDIT
personGet.setId(mPrefs.getString("id", null));
personGet.setName(mPrefs.getString("name", null));
personGet.setSurname(mPrefs.getString("surname", null));
// This is for saving to shared preferences.
mPrefs = context.getSharedPreferences(person.getId(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("name", person.getName());
editor.putString("surname", person.getSurname());
editor.putString("id", person.getId());
// This is for saving. the "id" is unique for all users –
SharedPreferences is just a Map of Key Value pairs
Yes this will save your id under the key “id”:
But doing this will override it as the key “id” is the same:
So when you ask for the sharedPreference “id” you will get
EDIT
Don’t forget to call commit(); on your sharedpreferences