Victor Grazi
Victor Grazi

Reputation: 16520

SharedPreferences not persistent

I am using a SharedPreferences in Android.Everything works great in the same session.

However once I relaunch the application, all of the preferences that were set from the previous session are lost.

Is there anything I need to specify to tell the SharedPreferences to hang around from run to run?

I am creating the preferences by calling

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

Then I set properties by e.g.

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

and I get it by

preferences.getString(key, defaultValue);

Thanks, Victor

Upvotes: 11

Views: 17412

Answers (8)

Alp Altunel
Alp Altunel

Reputation: 3443

A lot of time has been past but you may use

.apply() 

instead of

.commit()

that would be better. commit tries to write immediately but apply is asynchronous. here are the explanation from google docs

Unlike commit, which writes its preferences out to persistent storage synchronously, apply commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit while a apply is still outstanding, the commit will block until all async commits are completed as well as the commit itself.

Upvotes: 0

Metro Smurf
Metro Smurf

Reputation: 38385

The preferences will not be saved until you commit them, i.e.:

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

EDIT: The above has a subtle bug (from comments); you need to keep reference to the editor object, otherwise the above example that commits the value will create a new instance. Should be:

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

For reference, from the Android docs:

Note that you must call commit() to have any changes you perform in the Editor actually show up in the SharedPreferences.

Upvotes: 0

user3285870
user3285870

Reputation: 11

This is the code that works for me.

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

    }

Upvotes: 1

Phil Haigh
Phil Haigh

Reputation: 4591

If you are developing for API level ? and above then you should be using editor.apply() instead of editor.commit() when programatically modifying your preferences. editor.commit() has been deprecated, and editor.apply() will handle the actual persistence in the background, which editor.commit() does not do.

Note that editor.apply(), if it fails, does so silently.

Upvotes: 1

jeet
jeet

Reputation: 29199

SharedPreferences are persistent accross relaunch, restart, I think the problem is you are not commiting the preferences, use following to store values in preferences:

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

Upvotes: 30

Nixit Patel
Nixit Patel

Reputation: 4445

to get value in pref

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

to save vlue use onstoe or onPause methds

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

That works for me fine and healthy

Upvotes: 2

Brian Dupuis
Brian Dupuis

Reputation: 8176

You're likely not committing your changes. Set properties like so

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

Without the commit you're farting in the wind.

Upvotes: 24

deepak Sharma
deepak Sharma

Reputation: 1641

This is working for me please try:

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

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

Upvotes: 4

Related Questions