6

I have a similar problem like this, so I proceeded according to the proposed solution and added this line of code to onCreate:

PreferenceManager.setDefaultValues(this, R.xml.settings, false);

Unfortunately the problem still occurs, if the user hasn't altered the settings, still the default-value (true) from

mPreferences.getBoolean(String.valueOf(day_of_week), true)

is used instead of the default value from the XML.

One proposed to change the default-value parameter of getBoolean() to null, but this code crashes the app:

mPreferences.getBoolean(String.valueOf(day_of_week), (Boolean) null)

Any advice? Thanks in advance!

Community
  • 1
  • 1
JonEasy
  • 921
  • 10
  • 22
  • Do you set `android:defaultValue="default"` in your `settings.xml`? And this line should be added to your main activity `onCreate`, not your settings. – Knickedi Sep 28 '11 at 11:28
  • No, I set android:defaultValue to "true" or "false". the setDefaultValues() function is located in the onCreate of my main Activity, not in settings – JonEasy Sep 28 '11 at 14:49
  • @JhonnyR, I just wasted a few hours tracking this down too. I agree it is a bug. People should mod this post up to save people like me time in the future. – Brandyn May 31 '12 at 21:37

2 Answers2

6

Finally it works! I really put much time and effort into searching for the error and as soon as I post here, I find it out alone ~~ thanks guys for helping me with this one.

If ever anyoneelse has this problem, the solution goes like this: Change the default Value of getBoolean() from true to false like so:

mPreferences.getBoolean(String.valueOf(day_of_week), true) -> doesn't work, it is always true no matter what happened in the XML

mPreferences.getBoolean(String.valueOf(day_of_week), false) -> it works! It's the correct default Value from the XML

I really don't understand the logic into doing this, but now it works perfectly. Seems a bit like a bug to me.

JonEasy
  • 921
  • 10
  • 22
  • in the future please be sure to leave answers as *answers* not edits to the question please (I moved this one for you, you can view edit history on the Q for details) – Jeff Atwood Jun 01 '12 at 00:05
3

Set the third argument of setDefaultValues to true. So, PreferenceManager.setDefaultValues(this, R.xml.settings, true);

From the documentation:

public static void setDefaultValues (Context context, int resId, boolean readAgain)
If readAgain is false, this will only set the default values if this method has never been called in the past (or the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.
Note: this will NOT reset preferences back to their default values.

So, my understanding is:

  • If readAgain is false, it will read default values only once after the first run of app. If you add new property with default value to preferences, it will not initialized until you uninstall and install the app again.
  • If readAgain is true, it will read default values again and again on every function call. BUT, it will not reset values to default, if they already had been set or changed by the app.
GrAnd
  • 10,141
  • 3
  • 31
  • 43
  • +1 Good point, if this final hint doesn't solve the problem then nothing :-D – Knickedi Sep 28 '11 at 14:12
  • 1
    Tried that already too.. no improvement, it's still not the values from the XML – JonEasy Sep 28 '11 at 14:48
  • @JhonnyR Is it possible that these "incorrect" values are already saved as current settings values? In this case this method will never override them with defaults. Again, per documentation: Setting `readAgain` to `true` will NOT reset preferences back to their default values. For that functionality, use `getDefaultSharedPreferences(Context)` and clear it followed by a call to this method with this parameter set to `true`. In the future the clearing will not be needed as you start initialize default values correctly. – GrAnd Sep 28 '11 at 15:02
  • no, I cleared all saved data to ensure I'm working with default values. But the problem is now solved as pointed out in the Question post. Thanks for your help! – JonEasy Sep 28 '11 at 15:10
  • 1
    To see which settings are saved, you can grab the settings file with stored values(i.e using DDMS in Eclipse) and look inside. The file is located in `/data/data//shared_prefs`. – GrAnd Sep 28 '11 at 15:12