2012-02-09 63 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(); 
} 

    }