12

I found all answers here and tried all solutions, still my shared prefs are not persistent.

Here's my code:

public static void setActivated(boolean activated) {
    SharedPreferences sp = Utils.getContext().getSharedPreferences(
            USER_PREFS, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit(); 
    editor.putBoolean(ASD, activated);
    editor.commit();
}

public static boolean isActivated() {
    SharedPreferences sp = Utils.getContext().getSharedPreferences(USER_PREFS, Context.MODE_PRIVATE);
    return sp.getBoolean(ASD, true); 
}

I've tried also:

editor.clear();
editor.put ..
editor.commit();

I've also tried with

editor.apply();

I even tried with both .apply() and .commit() and no luck.

Another idea was to try using a different mode for the files:

...getSharedPreferences(USER_PREFS, Context.MODE_MULTI_PROCESS);

The problem is that the values saved are not persistent. If I close the app and then re-open it the values are all wrong.

Does anyone have any ideas? I would also mention that the problem is only on some devices, for example HTC One S, Samsung Galaxy S3 (I tested on a different S3 and it worked perfectly).

EDIT: I call the save on a button click listener and I call isActivated when I load the fragment (after onViewCreated()).

Thanks!

Razvan
  • 493
  • 5
  • 16
  • See this one :http://stackoverflow.com/questions/23654853/radiobuttons-and-spinners-in-shared-preferences/23655104#23655104 – Haresh Chhelana Jun 18 '14 at 10:05
  • Having the same problem... I have noticed that the problem only happens if I restart the app while it process is still alive (according to DDMS) - in this case it seems that some separate preference file is being used. If I kill the process in DDMS and restart afterwards - no problem... – Lez77 Jan 15 '16 at 10:57

3 Answers3

2
public abstract SharedPreferences.Editor clear()

Added in API level 1 Mark in the editor to remove all values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor. Note that when committing back to the preferences, the clear is done first, regardless of whether you called clear before or after put methods on this editor.

Returns Returns a reference to the same Editor object, so you can chain put calls together.

In my user preferences class I was getting a null value on some other strings and my code was something like this:

SharedPreferences sp = Utils.getContext()
    .getSharedPreferences(USER_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (session != null && !"".equals(session)) {
    sessionId = session;
    editor.putString(SESSION, sessionId).commit();
} else {
    sessionId = null;
    editor.clear().commit();
}

The editor.clear() was resetting all my other commits!

MikO
  • 18,243
  • 12
  • 77
  • 109
Razvan
  • 493
  • 5
  • 16
  • 1
    How does this help? Author said he had tried with and without editor.clear(). I have the same issue also without calling editor.clear() ... – Lez77 Jan 15 '16 at 10:56
1

Hi I think it should work. If clearing does not work, you could try the second option as detailed in my solution:

You have 2 options:

  1. Get shared preference value during the life-cycle of the activity.

  2. Call .clear before .commit

See my answer:

Android Persistent Checkable Menu in Custom Widget After Reboot Android

Community
  • 1
  • 1
-1

I don't know why, but it is working by just putting your prefs code inside the async task:

prefss = getSharedPreferences(ACCOUNT_PREFS_NAME, MODE_MULTI_PROCESS);
new AsyncSave(favNamesList).execute();
private static class AsyncSave extends AsyncTask<Void, Void, Boolean> {
    String favNamesList;

    AsyncSave(String favNamesList) {
        this.favNamesList = favNamesList;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        prefss.edit().putString("favNamesList", strings).apply();
        return null;
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
mhdjazmati
  • 4,152
  • 1
  • 26
  • 37