Reputation: 2941
String getString(String name, String defValue){...}
This is the definition of getString(...)
method of SharedPreferences
so I think it's possible if I run code below, it returns 1 two times:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
Timber.e(pref.getString("new", "1"));
Timber.e(pref.getString("new", "100"));
because at the first time its empty, so "1" will store, after that because of it has value ("1"), it will return it's value ("1") not default value ("100")
but it returns "1" and "100" and "new"
does not store in my pref file (located in data/data/...
)
Am I understanding it wrong or something goes wrong in this code?
Upvotes: 0
Views: 94
Reputation: 844
You must put"Something" to SharedPreferences.Editor, and commit them.
pref.getString wont store anything.
Refer to the links below: https://developer.android.com/reference/android/content/SharedPreferences.html https://developer.android.com/reference/android/content/SharedPreferences.Editor.html
Upvotes: 1
Reputation: 27211
Use can only get The data using getString.
to store data use Editor.commit();
Editor editor = settings.edit();
editor.putString("someKey", "someVal");
editor.commit();
only after that you can get this value.
String value = settings.getString("someKey", "someDefaultValueIfThisKeyNotUsedBefore");
In this example, you will recieve "someVal"
if commit
is used beforehand.
Upvotes: 1