The defaultValue is always being returned as 0, despite my attempt to return a different value when the pref key does not exist. I wonder why. Here’s my piece of code –
public static final String PREFS_NAME = "MyPrefsFile";
public static final String PREFS_OLD_TRIM = "trimName";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int trim_val = settings.getInt(PREFS_OLD_TRIM, 7);
I know for sure, in the initial run, that there is no pref key – PREFS_OLD_TRIM. So, i was expecting trim_val to get a value of 7. But it’s getting 0.
Am i missing anything? Thanks. I am targeting API Level 8 device set.
To be on the safe side try changing PREFS_OLD_TRIM to random value at every run – this will make you sure that there is no such key in your preference file.