【问题标题】:SharedPreferences not persistentSharedPreferences 不持久
【发布时间】:2012-03-01 14:40:35
【问题描述】:

我在 Android 中使用 SharedPreferences。在同一个会话中一切都很好。

但是,一旦我重新启动应用程序,之前会话设置的所有首选项都会丢失。

我需要指定什么来告诉 SharedPreferences 从一个运行到另一个运行吗?

我正在通过调用创建首选项

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

然后我通过例如设置属性

preferences.edit().putString(key, value);

我得到它

preferences.getString(key, defaultValue);

谢谢, 维克多

【问题讨论】:

  • 你写过 editor.commit() 吗?
  • 如果你是 Set<String 没有在应用会话之间保存,那是因为一个错误。使用此解决方法:stackoverflow.com/a/26315757/2371425.
  • 非常感谢 Sakiboy。在调用 commit 之后,我花了将近 4 个小时试图弄清楚为什么仍然在同一个上下文中验证更改,但随后切换到另一个上下文,我更新的值又恢复为旧值。 Android 请记录这些东西并做好你的工作,这样我就可以做我的了。在 cpu 上浪费了 4 个小时,因为出于某种原因,Android/Google 的文档很差......天哪!

标签: android session sharedpreferences


【解决方案1】:

这对我有用,请尝试:

private SharedPreferences mShared;
private Editor mEdit;
mShared = PreferenceManager.getDefaultSharedPreferences(this);

mEdit = mShared.edit();
mEdit.putString(key, value);
mEdit.commit();

【讨论】:

    【解决方案2】:

    您可能没有提交更改。像这样设置属性

    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(key, value);
    editor.commit();
    

    没有提交,你就在风中放屁。

    【讨论】:

    • 迅速升级:D
    【解决方案3】:

    在您提交之前不会保存首选项,即:

    // DON'T DO THIS... See Next Example
    preferences.edit().putString(key, value);
    preferences.edit().commit();
    

    编辑:上面有一个微妙的错误(来自 cmets);您需要保持对编辑器对象的引用,否则上面提交值的示例将创建一个新实例。应该是:

    Preferences.Editor edit = preferences.edit();
    edit.putString(key, value);
    edit.commit();
    

    供参考,来自Android docs

    请注意,您必须致电 commit() 进行任何更改 编辑器实际上显示在 SharedPreferences 中。

    【讨论】:

    • 这段代码是错误的,因为您提交的编辑器实例与您放置字符串的编辑器实例不同。
    • @ffleandro - 如果这段代码是错误的,那么这个问题的所有其他答案都是错误的。
    • 不,我见过很多正确的答案。您的答案的问题是,当您调用 preferences.edit() 时,您正在创建一个 SharedPreferences.Editor 对象。当您commit 时,您提交的实例与您称为putString 的实例不同,因此它不会持续存在。我建议您再次阅读您发布的docs
    【解决方案4】:

    SharedPreferences 在重新启动、重新启动时是持久的,我认为问题是您没有提交首选项,请使用以下将值存储在首选项中:

    Preferences.Editor edit=preferences.edit();
    edit.putString(key, value);
    edit.commit();
    

    【讨论】:

    • apply() 优于 commit(),因为它允许在后台进行写入
    【解决方案5】:

    在 pref 中获得价值

    SharedPreferences pref1 = getSharedPreferences(PREFS_NAME, 0);
    boolean silent = pref1.getString("silentMode", "...");
    

    要保存 vlue 使用 onstoe 或 onPause 方法

    SharedPreferences pref2 = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = pref2.edit();
    editor.putString("silentMode", "...");
    

    这对我来说很好,很健康

    【讨论】:

      【解决方案6】:

      如果您正在开发 API 级别?在以编程方式修改您的首选项时,您应该使用editor.apply() 而不是editor.commit()editor.commit() 已被弃用,editor.apply() 将处理后台的实际持久性,editor.commit() 不这样做。

      请注意,editor.apply(),如果失败,会静默执行。

      【讨论】:

      • 这是错误的:commit() 根本没有被弃用。 API 22 中的唯一注意事项是:如果您不关心返回值并且您在应用程序的主线程中使用它,请考虑改用 {@link #apply}。
      【解决方案7】:

      这是适合我的代码。

      package com.example.persistence;
      
      import android.os.Bundle;
      import android.widget.EditText;
      import android.app.Activity;
      import android.content.SharedPreferences;
      import android.content.SharedPreferences.Editor;
      
      
      public class MainActivity extends Activity {
      
      public static final String NOTE_PREFS="note";
      SharedPreferences msgSettings;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
           msgSettings = getSharedPreferences(NOTE_PREFS,0);
      
            EditText et= (EditText)findViewById(R.id.editText1);
            if (msgSettings.contains(NOTE_PREFS)) {
                  et.setText(msgSettings.getString(
                          NOTE_PREFS, "my note")); 
      
          }
      
      }
      @Override
      protected void onPause() {
            EditText et= (EditText)findViewById(R.id.editText1);
            String b = et.getText().toString();
            Editor editor = msgSettings.edit();
            editor.putString(NOTE_PREFS,b);
              editor.commit();
          super.onPause();
      }
      @Override
      protected void onDestroy() {
           EditText et= (EditText)findViewById(R.id.editText1);
            String b = et.getText().toString();
            Editor editor = msgSettings.edit();
            editor.putString(NOTE_PREFS,b);
              editor.commit();
          super.onDestroy();
      }
      
          }
      

      【讨论】:

        【解决方案8】:

        很多时间过去了,但你可以使用

        .apply() 
        

        而不是

        .commit()
        

        这样会更好。 commit 尝试立即写入,但 apply 是异步的。这是来自谷歌文档的解释

        与 commit 不同,它将首选项同步写入持久存储,apply 将其更改立即提交到内存中的 SharedPreferences 但开始异步提交到磁盘,并且您不会收到任何失败的通知。如果此 SharedPreferences 上的另一个编辑器在应用仍未完成时执行常规提交,则提交将阻塞,直到所有异步提交以及提交本身都完成。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-09
          • 1970-01-01
          • 2020-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多