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