I’ve an app on each the App Retailer (iOS – Swift) and Play Retailer (Android – Java). The app makes use of UserDefaults.normal for iOS and SharedPreferences for Android to retailer a couple of settings.
Each of those implementations work completely. The apps could be launched and stop lots of of instances over the course of months and all settings persist.
Once I push an replace to the shops and set up the updates on a number of gadgets, the settings are deleted. From what I’ve learn on-line, UserDefaults and SharedPreferences are alleged to persist between app updates and will solely be deleted when the app is uninstalled. Nonetheless, this isn’t the case for me. I’ve additionally discovered a number of posts concerning this challenge on many various websites (together with this one) however there may be by no means a solution.
Is there some form of setting, flag, code, or anything that must be set (or not set) with a purpose to make sure that UserDefaults and SharedPreferences persist after an app replace?
Are UserDefaults and SharedPreferences what I needs to be utilizing for this objective? If not, what ought to I be utilizing? I’ve already tried to save lots of a serialized object to file and an xml file however these have been deleted after an app replace as nicely. (See: File Deleted After Replace)
I don’t want any safety for these settings. I simply want a strong solution to have the settings persist between app updates.
Right here is the related code for iOS adopted by Android. After an app replace, the tutorial display is at all times proven. All different settings are additionally defaults.
class Settings {
static let shared = Settings()
var tutorialShown = false
non-public init() {
let defaults = UserDefaults.normal
if defaults.worth(forKey: "tutorialShown") == nil {
defaults.set(false, forKey: "tutorialShown")
}
tutorialShown = defaults.bool(forKey:"tutorialShown")
//different settings omitted for brevity
}
func save() {
let defaults = UserDefaults.normal
defaults.set(tutorialShown, forKey:"tutorialShown")
//different settings omitted for brevity
}
}
//someplace within the code
Settings.shared.tutorialShown = true
Settings.shared.save()
//someplace within the code
if(Settings.shared.tutorialShown) {
sceneName = "TitleScene"
} else {
sceneName = "WelcomeScene"
}
//And for Android
public class Settings {
//occasion
non-public static Settings occasion;
non-public static Lock instanceLock = new ReentrantLock();
public boolean tutorialShown;
public static Settings getInstance() {
instanceLock.lock();
attempt {
if (occasion == null) {
occasion = new Settings();
}
return occasion;
} lastly {
instanceLock.unlock();
}
}
non-public Settings() {
Preferences prefs = Gdx.app.getPreferences("packageNameHere");
tutorialShown = prefs.getBoolean("tutorialShown", false);
//different settings omitted for brevity
}
public void save() {
Preferences prefs = Gdx.app.getPreferences("packageNameHere");
prefs.putBoolean("tutorialShown", tutorialShown);
prefs.flush();
}
}
//someplace within the code
Settings.getInstance().tutorialShown = true;
Settings.getInstance().save();
//someplace within the code
if(Settings.getInstance().tutorialShown) {
this.setScreen(new TitleScreen(this));
} else {
this.setScreen(new WelcomeScreen(this));
}