Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SharedReferences提交和回滚_Android_Settings_Sharedpreferences_Commit_Rollback - Fatal编程技术网

Android SharedReferences提交和回滚

Android SharedReferences提交和回滚,android,settings,sharedpreferences,commit,rollback,Android,Settings,Sharedpreferences,Commit,Rollback,以下是提交和回滚SharedReferences的预期/合理方法,并且通常使用它吗 public class Settings { private static final String PREFS_NAME = "Settings"; private static SharedPreferences preferences = null; private static SharedPreferences.Editor editor = null; public static void in

以下是提交和回滚SharedReferences的预期/合理方法,并且通常使用它吗

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

}
这是stackoverflow编辑器强制执行的更多细节。我真的不知道还应该说些什么


非常感谢专家的反馈。如果may snipped是合理的,那么它可以比我在这里找到的所有其他线程更好地解释。

根据我的理解,以下方法只有一个变化。因为如果您忘记初始化首选项,您将得到空指针异常

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

                                        }
保持事物的最佳方式是“使用偏好活动”。查看示例并阅读有关它们的在线文档。使用EditTextPreference自动保存值

首先,没有人会使用共享首选项来保存用户id和密码。因为共享首选项是键值对。对于关键电子邮件,您只能有一个相应的值。这里您想要的是:-对于密钥电子邮件多个值,对于密钥密码也多个值


如果你想做这样的事情,有一个解决方案使用电子邮件id(xyz@xyz.com)as键。和密码作为密钥的值(xyz@xyz.com).

你说“回滚”是什么意思?是否要将已提交的更改还原到prefs?或者您想删除从未提交的更改?或者完全是别的什么?将commit()操作作为一个单独的函数对我来说很危险。这意味着你可能会忘记调用它,并且pref不会被保存,我宁愿在pref被更改后直接在任何地方使用它。类是否也应该是静态的?那么您就不必创建它的实例了