Question

I have a EditTextPreference and I don't know when that string get saved to the Shared Preferences file.

If I start with a null string, and change it to "Hello", when does this update get saved? Do I have to save it manually?

Was it helpful?

Solution

I have a EditTextPreference and don't know when it commits that string to the Shared Preferences file

If you take a look at the EditTextPreference documentation, the text gets saved upon calling the setText(String) method. This method commits saves the text to the SharedPreferences. The preferences will never be updated until you call that method. For example...

EditTextPreference mPrefs = ...

//perform any manipulations on the string, not saved until you call setText()
String mText = "2";
mText += " + 2";
mText += " = 4";

// saves "2 + 2 = 4" to SharedPreferences
mPrefs.setText(mText);

OTHER TIPS

When you commit it, by calling either Editor.commit() or Editor.apply().

See the documentation

Looking at the source for EditTextPreference.java, the String is persisted in the setText() method.

So, it'll be committed to the SharedPreferences file after the text is changed.

when u edit your prefrence you may use the following code:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);


Editor edit = sp.edit();
edit.putString("Preference_Label", variable_name);
edit.commit(); // this commits the edit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top