Reputation: 70406
I have implemented some app preferences for my app. For strings they work great but not for boolean values. E.g.
public class MdSharedPrefs {
public final static String PREFS_NAME = "prefs";
public static boolean getSSFlag(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
return prefs.getBoolean(context.getString(R.string.pref_key_ss), false);
}
public static void setSSFlag(Context context, boolean newValue) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
Editor prefsEditor = prefs.edit();
prefsEditor.putBoolean(context.getString(R.string.pref_key_ss),
newValue);
prefsEditor.commit();
}
}
And this requires
<string name="pref_key_ss"></string>
in strings.xml. When I remove this line the project wont compile. But when I leave it this boolean pref does not work. When I click it and go back to main screen and then go back to prefs the value is not checked.
However for strings it works fine.
Any ideas whats wrong?
Upvotes: 0
Views: 186
Reputation: 22493
in my application i am using like this it's working fine
declaration
private static final String SUNDAY_ON = "sunday_selected";
getting
state = prefs.getBoolean(SUNDAY_ON, false);
storing like this
editor.putBoolean(SUNDAY_ON, value);
Upvotes: 2