views:

40

answers:

2

The documentation for the PreferenceActivity states "These preferences will automatically save to SharedPreferences as the user interacts with them." That being the case, if I subclass PreferenceActivity, invoke addPreferencesFromResource in onCreate, set some preferences in the UI I would expect that any changes made to my preferences should automagically be saved; and when I return to my preference screen later all preferences should be restored.

The reason I'm asking is CheckBoxPreference is not behaving as expected, assuming my understanding is correct of course. I would expect if I have a CheckBoxPreference the users selection should be saved automatically upon change. Is this not the case?

So can anyone please confirm my understanding or correct me if necessary?

I can create a custom preference screen where I handle all the changes in code but I'd prefer to use the recommended approach. I 'm just wondering if there is a bug.

Suffice it to say I am a wee bit frustrated at the moment.

BTW I'm developing against v2.0 of the api.

+1  A: 

I would expect if I have a CheckBoxPreference the users selection should be saved automatically upon change. Is this not the case?

This works fine, if they make a positive selection. If they visit your activity and do not touch a CheckboxPreference, it will remain at the default.

CommonsWare
So if I make a positive selection and then use the back button the positive selection ought to be saved, correct? Because if that is the designed behavior it's not happening. The positive selection is gone when I return to the Activity. If I leave the activity via the home button and later return then the positive selection IS displayed. The only code I've got is in onCreate where I call addPreferencesFromResource. Sorry if this is all very basic but I'm just trying to get a handle on Android's preference framework and this behavior has me a tad confused.
Trip
@Trip: "So if I make a positive selection and then use the back button the positive selection ought to be saved, correct?" -- yes. "Because if that is the designed behavior it's not happening. The positive selection is gone when I return to the Activity." -- here is a sample project where this all works fine: http://github.com/commonsguy/cw-android/tree/master/Prefs/Dialogs/
CommonsWare
@CW Thanks for the code sample/reference. A basic test project functions as I would expect so I've got something off in my development work that I need to ferret out. Appreciate the help!
Trip
A: 

It turns out my error was quite simple.

I'm using strings.xml to store the name of the checkbox key and dereferencing that at run time like we do with labels etc. Well the value this was resolving to had a space in it so of course it wouldn't work.

The errant string entry was something like this:

<string name="key_vibrate_pref">Vibrate preference</string>

And it should have been something like this:

<string name="key_vibrate_pref">ckPrefVibrate</string>

DOH!!!!

Trip