Home » Android » android – SharedPreferences not persistent

android – SharedPreferences not persistent

Posted by: admin May 14, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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.

Answer:

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

Answer:

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

Answer:

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.

Answer:

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

    }

Answer:

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.