What to do if the TYPE of preference changed in Android Preferences? For instance if Boolean
changed into ListPreference
?
Really noone at Google thought about Preference Migrations?
The only sensible way for now seems to version preferences and mark for removal preferences that changed with a given version..?
Try to read key with new data type, in case of ClassCastException
exception delete "old" key, and create new key with same name but new type. Something like this:
SharedPreferences prefs;
String key = "key";
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.contains(key)) {
// key exists, so tetermine it's type
try {
prefs.edit().get<old_type_name>(key, <default_old_type_value>);
} catch (Exceprtion e) {
if (e instanceOf ClassCastException) {
prefs.edit().remove(key).apply();
}
}
}
// we are here if no key exists or key removed
prefs.edit().put<new_type_name>(key, <new_type_value>).apply();
and if needed do check if (prefs.contains(key)) ...
only once on first app start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With