Reputation: 461
I would like to change a SharedPreference from Boolean to String. The default value was true and will be "enabled". This is not a problem because it will be set automatically. But how can I check if it was set to false and then set the new preferece to "disabled"? Is there a method called during update of the app where I could migrate this preference? If I don't migrate it, the state of this preference is lost during update...
Upvotes: 1
Views: 1866
Reputation: 12207
SharedPreferences preferences = // obtain it
String pref;
try {
pref = preferences.getString("yourPref", "enabled");
} catch (ClassCastException e) {
// this means the pref is stored as a boolean
boolean boolPref = preferences.getBoolean("yourPref", true);
// store it instead as a String
pref = boolPref ? "enabled" : "disabled";
preferences.edit().remove("yourPref").putString("yourPref", pref).commit();
}
Upvotes: 3
Reputation: 8023
You could add a simple method in the first activity which'll open, to update the preferences values for you. Also, add a check so that it executes only for the first time.
Upvotes: 1