Android libgdx preferences not working

て烟熏妆下的殇ゞ 提交于 2019-12-02 08:14:17

问题


Probably a stupid question, but what do I have to change to make this code work:

package com.hobogames.WizardsDuel;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class GameData {
    public static String version = "1.0.0";

    public static String data[] = {"version"};
    public static final int VERSION = 0;

    public static Preferences prefs = Gdx.app.getPreferences("WizWars");
    public static boolean played = (!prefs.get().isEmpty());

    public static String getString(int key){
        return prefs.getString(data[key]);
    }

    public static void storePrefs(){
        prefs.putString(getString(VERSION),version);
        prefs.flush();
    }
}

The part in particular that isn't working is that "played" is false every time on an android device. On desktop it's true after the first play.


回答1:


I suspect you're getting bitten by the static state behaving differently in Android and on your Desktop. Are you sure the VM exits when you exit the app on Android? If you restart your app quickly on Android, the system will reuse the same Dalvik VM for the new Activity. Since the static boolean is already initialized, the initialization isn't re-run. If you remove the static so this stuff gets re-run when GameData is created (presumably instances of it are not being stored in static variables) you should get farther.

See http://bitiotic.com/blog/2013/05/23/libgdx-and-android-application-lifecycle/



来源:https://stackoverflow.com/questions/17100373/android-libgdx-preferences-not-working

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