2012-02-09 69 views
8

我在Android中使用SharedPreferences。在同一会话中,一切都很好用。SharedPreferences不持久

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

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

我通过调用

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 

创建喜好然后,我通过设置属性例如

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

,我通过

preferences.getString(key, defaultValue); 

感谢得到它, 维克多

+2

你有没有写过editor.commit()? – Richa 2012-02-09 04:58:04

+1

如果你在应用程序会话之间没有保存'Set Sakiboy 2016-09-08 02:04:29

回答

26

SharedPreferences是持久的防空火炮重新开张,重新启动,我认为这个问题是你是不是commiting喜好,使用下列存储值偏好:

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

'apply()'比'commit()'更好,因为它允许在后台进行写入 – levengli 2017-03-26 14:28:14

2

这是为我工作的请尝试:

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

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

你可能不会提交你的修改。设置属性像这样

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

没有提交你放风肆虐。

+23

+1在风中放屁。 – StackOverflowed 2012-04-11 22:06:52

+0

很快就升级了:D – 2014-04-01 09:09:44

0

的偏好将不会被保存,直到您提交它们,即:

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

编辑:上面有个微妙的问题(从评论);您需要保持对编辑器对象的引用,否则上述提交该值的示例将创建一个新实例。应该是:

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

作为参考,从Android docs

请注意,您必须调用commit()有你的 进行任何更改编辑居然在SharedPreferences露面。

+0

这段代码是错误的,因为您在与您放置字符串的实例不同的编辑器实例中提交。 – ffleandro 2013-07-13 19:58:49

+0

@ffleandro - 如果此代码错误,那么对于此问题的其他答案也是如此。 – 2013-07-14 00:07:07

+2

没有人,我见过很多正确的答案。你的答案的问题是当你调用'preferences.edit()'时,你正在创建一个'SharedPreferences.Editor'对象。当你提交'''''''''''''''''''''''''''''''''''我建议你再读一遍你发布的'docs'。 – ffleandro 2013-07-14 13:53:13

1

在PREF

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

保存vlue使用onstoe或在onPause methds

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

这对我来说很好,很健康

1

如果您正在开发API级别?和以上,那么你应该使用editor.apply()而不是editor.commit()当编程修改您的偏好。 editor.commit()已被弃用,并且editor.apply()将处理后台中的实际持久性,editor.commit()不会执行此操作。

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

+0

这只是错误的:commit()根本不被弃用。 API 22中唯一的注意事项是:如果您不关心返回值,并且您在应用程序的主线程中使用了此值,请考虑使用{@link #apply}。 – 2015-08-23 17:02:20

1

这是适合我的代码。

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

    }