0

Is the following the intended/reasonable way to commit and rollback SharedPreferences, and to use it in general?

public class Settings {

private static final String PREFS_NAME = "Settings";
private static SharedPreferences preferences = null;
private static SharedPreferences.Editor editor = null;

public static void init(Context context) {
    // Activity or Service what ever starts first provides the Context
    if (preferences == null)
        // getSharedPreference because getPreferences is a method of Activity only (not Service or Context)
        preferences = context.getSharedPreferences(PREFS_NAME, 0);
        editor = preferences.edit();
}

public static String getEmail() {
    return preferences.getString("email", null);
}

public static void setEmail(String email) {
    editor.putString("email", email);
}

public static String getPassword() {
    return preferences.getString("password", null);
}

public static void setPassword(String password) {
    editor.putString("password", password);
}

public static void save() {
    editor.commit();
}

public static void rollback() {
    editor = preferences.edit();
}

}

This is more detail as enforced by stackoverflow editor. I really don't know what else should be said about this.

Experts' feedback is much appreciated. And if may snipped is reasonable it could well as better explanation then all other threads I have found here.

user2583621
  • 165
  • 12
  • what do you mean specifically when you say "rollback"? You want to revert an already committed change to the prefs? Or you want to remove a change that was never committed? or something else entirely? – FoamyGuy Sep 10 '13 at 16:05
  • it seams dangerous to me to have the commit() operation as a separate function. means you could forget to call it and the pref would not be saved, i would rather have it directly anywhere after a pref is change. Also should the class not also be static? then you dont have to create an instance of it – Jock Mahon Sep 10 '13 at 16:10

1 Answers1

0

There is only one change in the following method from my understanding. Because if u forget to initialize preference ,u will get null pointer exception.

         public static void rollback(Context context) {
                                        if (preferences == null)
                                        // getSharedPreference because getPreferences is a method of Activity only (not Service or Context)
                                        preferences = context.getSharedPreferences(PREFS_NAME, 0);
                                        editor = preferences.edit();

                                        }

Best way to have things persisted is "USE OF PREFERENCE ACTIVITY". See examples and read online docs about them. Use EditTextPreference for automatically persisting values.

First thing is that no body ever uses shared preference to save user id and password . Because shared preference is key-value pair. For key Email you can have only one respective value. Here what you want is :- for key Email multiple values and for key password also multiple value.

There exist one solution if u want to do something like this. Use email id (xyz@xyz.com) as key . And password as the value of key(xyz@xyz.com).

Pranav Jadav
  • 763
  • 5
  • 11