问题
I've created Android game in Libgdx engine... After some time I've noticed that game options that I'm saving into prefs are lost after killing the app in some app killer, or if I do "Force Close" in Android Settings...
Is there a way to prevent this? I've never done "Clear Data", just "Force close"...
Is there a good alternative to prefs?
Really weird problem...
This is how I get and put values into prefs...
/** Shared preferences */
public static final Preferences prefs = Gdx.app.getPreferences("MyPreferences");
/** Earned experience points */
public static int experiencePoints=SettingsManager.prefs.getInteger(EXPERIENCE_POINTS, 0);
// Store new experience points
SettingsManager.prefs.putInteger(EXPERIENCE_POINTS,experiencePoints);
Help...
回答1:
- You're only updating the preferences object in memory
- You're storing the preferences in a static field.
To fix the first problem, you'll want to "flush" the preferences to disk:
SettingsManager.prefs.flush();
You should do this when the prefs are consistent, and ideally not on the main UI thread. See https://code.google.com/p/libgdx/wiki/Preferences#Flushing
The static field may cause problems when your Activity (Libgdx app on Android) exits but the Dalvik VM underlying it does not and then a new Activity starts in the same VM. I'm not sure if Preferences objects store any internal references to Libgdx state that might go stale between activities, but its better to be safe and avoid static fields in Android games unless you really understand the consequences.
来源:https://stackoverflow.com/questions/18411126/android-libgdx-prefs-getting-lost