打开 SharedPreferences 和编辑器不好吗

Is it bad leaving SharedPreferences & Editor open

例如你有这个 class

private SharedPreferences sp
private SharedPreferences.Editor editor

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

这样做不好吗?

共享首选项没问题,编辑器不行。 只要您不调用编辑器的提交或应用,这些值就不会写入 preferences.file 并且我认为您在从 sp.

读取时也不会获得这些值

所以作为一种习惯,在写偏好值时总是这样做(Java)

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

或putInt、putBoolean等