I would like to save the player’s best score+2 other values in the game’s preferences file. Now, I know I need to call the flush() method in order for the values to get persisted, but from some reason they are not saved at all.
For instance, let’s say I turn off the music in the game. When I exit the app and then start it, the music is on.
I call the flush method in my preferences object but it still doesn’t save the value into the preferences file.
Here is my settings class:
public final class Settings {
private boolean soundOn;
private boolean musicOn;
private int bestScore;
private static Settings instance = new Settings();
public static Settings getInstance() {
return instance;
}
private Settings() {
soundOn = ScreenHandler.getInstance().getGame().getPreferences().getBoolean(Values.KEY_SOUND, true);
musicOn = ScreenHandler.getInstance().getGame().getPreferences().getBoolean(Values.KEY_MUSIC, true);
bestScore = ScreenHandler.getInstance().getGame().getPreferences().getInteger(Values.BEST_SCORE, 0);
}
public void setSoundOn(boolean soundOn) {
this.soundOn = soundOn;
ScreenHandler.getInstance().getGame().getPreferences().putBoolean(Values.KEY_SOUND, soundOn);
}
public void setMusicOn(boolean musicOn) {
this.musicOn = musicOn;
ScreenHandler.getInstance().getGame().getPreferences().putBoolean(Values.KEY_MUSIC, musicOn);
}
public void setBestScore(int bestScore) {
this.bestScore = bestScore;
ScreenHandler.getInstance().getGame().getPreferences().putInteger(Values.BEST_SCORE, bestScore);
}
public void save() {
ScreenHandler.getInstance().getGame().getPreferences().flush();
}
public boolean isSoundOn() {
return soundOn;
}
public boolean isMusicOn() {
return musicOn;
}
public int getBestScore() {
return bestScore;
}
}
Whenever I set the values in the game, I call the save method which invokes the flush method in the preferences object.