views:

84

answers:

1

I'm trying to keep a boolean preference in sync with a value in a content provider, and succeeding nicely except for this issue. When I set the preference in my PreferenceActivity's onResume() method (because the value may have been changed in the content provider), the checkbox in the view still reflects the pre-onResume value.

As far as I'm concerned, this preference could be entirely virtual and not persistent, but if it needs to persist for the wheels to turn that's fine, too. The end goal is a check box in my app's preferences that reflects and updates the value in the content provider.

A: 

I suspect the PreferenceActivity first sets the checkbox in the onRestoreInstanceState() method, and then your onResume() method is called later in the lifecycle. Though you change the preference value, the checkbox sync code is already finished.

So maybe you can override onRestoreInstanceState(), update the preference value, and then call super.onRestoreInstanceState() last.

Eric Burke
Does onRestoreInstanceState() get called every time the Activity returns to visibility? I had the impression that it could go straight from onPause() to onResume().
loganj
No dice. Looks like onRestoreInstanceState() isn't being called when coming back from pause.
loganj
It looks like there's no change listener registered with the `SharedPreferences`, so when you edit the `Preference` directly the UI layer never hears about it. I'll stay on it.
loganj