0

I save the state of a boolean in onPause method. I have tried using both editor.apply() and editor.commit() but still the values are not saved. Here is the onPause method:

@Override
protected void onPause() {
            SharedPreferences.Editor editor = preferences.edit();
            editor.clear(); //I have tried omitting it
            editor.putBoolean(MUTE_TAG,mute);
            editor.apply(); // I tried editor.commit() but with no effect
            boolean mute1 = preferences.getBoolean(MUTE_TAG,false);
            Log.d(TAG,"Values:\n"+ "mute1 " + mute1 + " mute);
            super.onPause();
        }

Note: When I try to access the value from the SharedPreferences again from different activity, it isn't updated. Here is the setup code

public static boolean mute;
public static final String PREFS = "prefs";
    init(Context context) {
    preferences = context.getSharedPreferences(PREFS,MODE_PRIVATE);
    mute = preferences.getBoolean(MUTE_TAG,false);
        }

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG,"MUTE PRESSED");
            mute = isChecked;
            break;
}
Rishi Raj
  • 1
  • 3
  • No, `preferences = context.getSharedPreferences(PREFS,MODE_PRIVATE); mute = preferences.getBoolean(MUTE_TAG,false);` – Rishi Raj Jul 10 '17 at 15:33
  • remove clear and call super.onPause() at the beginning. if you use apply() or commit() does not matter in your case. – Willi Mentzel Jul 10 '17 at 15:33
  • Pls, show me how you retrieve the preferences Object. – Willi Mentzel Jul 10 '17 at 15:39
  • `public static void init(Context context) { preferences = context.getSharedPreferences(PREFS,MODE_PRIVATE);` I want the mute field to be static and call the init from Application class to have the mute which is public static field accesible to all classes – Rishi Raj Jul 10 '17 at 15:40
  • Look at the edited question – Rishi Raj Jul 10 '17 at 16:22

1 Answers1

0

What about after saving? Does the logcat show the current/expected value?

Here is what has worked for me:

Before : private boolean mute = true; but after sending the app to background. the result showed it true. While the default value is false;

COPIED YOUR CODE AS-IS

ONLY PROBLEM: Unclosed string literal

    @Override
    protected void onPause() {
        SharedPreferences preferences = getSharedPreferences("SHARED_PREFERENCES_NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        mute = !mute ;
        Log.d(TAG, "UPDATED MUTE VALUE :: " + mute);

        editor.clear(); //I have tried omitting it
        editor.putBoolean(MUTE_TAG,mute);
        editor.apply(); // I tried editor.commit() but with no effect
        boolean mute1 = preferences.getBoolean(MUTE_TAG,false);
        Log.d(TAG,"Values:\n"+ "mute:  " + mute1 + " mute");
        super.onPause();
    }

SOLUTION #2

It is connected to this What's the difference between commit() and apply() in Shared Preference . Try using commit again, commit will block super.onPause from executing thus making sure it saves

SOLUTION #3

Your activities are calling onPause after onCreate thus saving back the default mute value. Therefore, Create a class & add STATIC PUBLIC /PUBLIC STATIC variables that will store the mute value & only save it when an activity is being destroyed. This static variable will not change it's value across activities

  • Is mute always returning **false**? If so, most likey onPause is being called other times. Could you save the mute status in the method `onCheckedChanged`? This might be a better strategy, there will be little if any performance effect of this – Ephraim Kigamba Jul 10 '17 at 16:39
  • It looks to be somewhat random. Sometimes, it is reversed, other times it isn't. – Rishi Raj Jul 10 '17 at 16:40
  • Try saving the value in `onCheckedChanged`. The activity(s) might be calling onPause when the activity(s) starts – Ephraim Kigamba Jul 10 '17 at 16:41
  • Turns out that in the preferences.xml file, only 2 preferences are being saved at one time. Is there any reason for this? – Rishi Raj Jul 10 '17 at 17:11
  • Sorry, what do you mean? – Ephraim Kigamba Jul 10 '17 at 17:13
  • I have 4 switches which are implemented in that way and only the state of two of them are getting saved, no matter how many times I switch them or restart the activity and yes they all have different onCheckedChangeListeners. Here is an [image](https://vgy.me/197zHN.png) of what I mentioned – Rishi Raj Jul 10 '17 at 17:19
  • Do you have a repository on Github so that I can see all the code that's not working as expected? – Ephraim Kigamba Jul 10 '17 at 17:21
  • **MUTE_TAG, TTS_TAG, COUNTDOWN_TAG, LOG_TAG** all have the same tag name. It should be different for each – Ephraim Kigamba Jul 10 '17 at 17:30