Android libgdx prefs getting lost

早过忘川 提交于 2019-12-12 19:24:24

问题


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:


  1. You're only updating the preferences object in memory
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!