Do I need to call edit SharedPreferences everytime before I change the preferences and commit or can I do that just once in the costructor?
Is this correct ?
public class MyPreferencesClass {
private Context ctx;
private static SharedPreferences settings;
private static SharedPreferences.Editor editor;
// Constructor Class //
public MyPreferencesClass(Context context) {
this.ctx = context;
settings = ctx.getSharedPreferences(CUSTOM_PREFS_NAME, 0);
editor = settings.edit(); // Called Only once in constructor not not everytime in edit methods
}
public static void setSomePref1(Boolean boolValue) {
// editor = settings.edit(); Not required everytime
editor.putBoolean(PREFS_1, boolValue);
editor.commit();
}
public static void setSomePref2(Boolean boolValue) {
editor.putBoolean(PREFS_2, boolValue);
editor.commit();
}
...
}
you should be able to use SharedPreferences.Editor instance as long as it’s valid so no need to call SharedPreferences#edit() every time.