1

I am trying to save some String data using SharedPreference.

Here is my code

// SAVE
SharedPreference mySharedPref = new SharedPreference("Data", 0);
mySharedPref.edit().putString("hello", "world");
mySharedPref.edit().putString("my", "code");
mySharedPref.edit().commit();

// LOAD
String str = mySharedPref.getString("hello", ""); // I expect "world"

But it only has an empty string!

Meanwhile,

SharedPreference.Editor editor = mySharedPref.edit();
editor.putString(...);
editor.commit();

This worked....

I thought mySharedPref.edit() returns a reference and makes the code above and below the same.

I am confused now. X(

Eddie
  • 761
  • 3
  • 9
  • 22
  • `edit()` method in `SharedPreference` returns NEW INSTANCE of the `Editor` interface. I found the comment explaining it in `SharedPreferences.java` file came with sdk. – Eddie Feb 18 '16 at 03:09

3 Answers3

1

See this solution

Setting values in Preference:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.

}

Community
  • 1
  • 1
Inducesmile
  • 2,475
  • 1
  • 17
  • 19
1

in your code:

// SAVE
SharedPreference mySharedPref = new SharedPreference("Data", 0);
mySharedPref.edit().putString("hello", "world");
mySharedPref.edit().putString("my", "code");
mySharedPref.edit().commit();

You called edit() each time you put a String but you didn't call commit() after it so your changes is not saved. You should change to this:

// SAVE
SharedPreference mySharedPref = new SharedPreference("Data", 0);
mySharedPref.edit().putString("hello", "world")
    .putString("my", "code");
    .commit();
Rey Pham
  • 605
  • 3
  • 11
0

edit() method in SharedPreference returns NEW INSTANCE of the Editor interface. I found the comment explaining it in SharedPreferences.java file came with sdk.

Any actions performed on the new instance will be independent from other instances.

In the first part of my code, by calling edit() multiple times, I was performing actions on each independent instance.

It was interesting to notice that edit() returns a new instance instead of a reference.

/** * Create a new Editor for these preferences, through which you can make * modifications to the data in the preferences and atomically commit those * changes back to the SharedPreferences object. * *

Note that you must call {@link Editor#commit} to have any * changes you perform in the Editor actually show up in the * SharedPreferences. * * @return Returns a new instance of the {@link Editor} interface, allowing * you to modify the values in this SharedPreferences object. */ Editor edit();

Eddie
  • 761
  • 3
  • 9
  • 22