문제

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?

도움이 되었습니까?

해결책

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);

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top