15

I'm trying to force PreferenceActivity to refresh. Does anyone know how to do this?

I have a ResetDefaultsPreference class that subclasses Preference and, when clicked, is supposed to reset to defaults all preferences whose keys start with a certain prefix. It works, but when I hit the reset preference, none of the preferences in that screen update until I back out of the screen and go back in. (That works for some custom color preferences, but even that doesn't work for some ListPreferences - for those I have to leave and re-enter the PreferenceActivity itself for the updated values to be shown.)

I tried to fix this by getting the root view and invalidate()ing it, but that doesn't seem to work. Here's the line to refresh the display. It's in part of a Preference subclass that keeps the Context it was created with in mContext.

((Activity)mContext).findViewById(android.R.id.content).invalidate();

This happens after the preference values have been changed and committed. (The values change, but the display doesn't.) Does anyone know how I can force PreferenceActivity to refresh itself?

tmandry
  • 1,295
  • 16
  • 33
  • the full source code of the class, for those interested, is available [here](http://pastebin.com/ZHmSThUH). – tmandry Jun 19 '11 at 01:16
  • 1
    I found a hack that partially works: after resetting defaults, call `setSummary(getSummary() + " ");` or something to that effect from the `Preference` to change the preference's summary, which causes the view to refresh. This works for the custom color preferences, but does _not_ update the values in `ListPreference` dialogs. – tmandry Jun 21 '11 at 05:13
  • Did you try `notifyChanged()` and `notifyHierarchyChanged()` of the `Preference`? – Stan Nov 07 '13 at 18:11

1 Answers1

13

I don't know of a way to "refresh" the PreferenceActivity, but you can create the illusion to the user. It will will close and reopen the activity with no animations, so it will appear as if the value just changes.

Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
stipe108
  • 1,640
  • 1
  • 16
  • 20