Is it bad leaving SharedPreferences & Editor open

sero583 :

e.g. you have this class

private SharedPreferences sp
private SharedPreferences.Editor editor

protected void onCreate(..) {
this.sp = ...
this.editor = this.sp.edit();
}

Is it bad to do that?

Ridcully :

The shared preferences are ok, the editor isn't. As long as you do not call editor's commit or apply, the values are not written to the preferences.file and I think you also will not get the values when reading from sp.

So as a habit, always do this when writing a preference value (Java)

sp.edit().putString(key, value).apply()

Or putInt, putBoolean etc.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477955&siteId=1