Refresh DialogPreference without closing PreferenceActivity

304 views Asked by At

I have a PrefrenceActivity which includes DialogPreference with my custom layout (edittext and checkbox).

When my DialogPrefernce is called for the first time, its onSetInitValue is called, I get values from SharedPreferences and then in onBindDialogView I update my edittext and checkbox.

Then I change the edittext value, (positive) close the dialog and save the value to SharedPreferences in onDialogClosed.

Great work so far.

Unfortunately, when I open the dialog again, the onSetInitValue is not called and the values remains unchanged. I've found out the onSetInitValues is called only after closing the PrefrenceActivity and reopening it again.

I hacked it using a flag (if onSetInitValues is called I set flag which I check in onBindDialogView - if it's not set I get values from SharedPreferences).

This is ugly solution. Is there a way to "rerender" the dialog or call onSetInitValues when the Preferences wasn't closed?

1

There are 1 answers

0
AAryz On

Had exactly same problem and unfortunately only found a more or less clean workaround:

initialValue = getPreferenceManager().getSharedPreferences().getInt(getKey(), DEFAULT_VALUE);

Call the above in your onCreateDialogView method and replace getInt and DEFAULT_VALUE with your desired type and default constant respectively.

This should get up-to-date preference value every time you open the dialog but may god be my witness Android doesn't stop shocking me with it's contorted and needlessly complicated APIs. Documentation is silent on this one as well AFAICT.