tmandry
tmandry

Reputation: 1375

refreshing views of Preferences when using PreferenceActivity

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?

Upvotes: 15

Views: 3576

Answers (1)

stipe108
stipe108

Reputation: 1650

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);

Upvotes: 13

Related Questions