Android libgdx 首选项不起作用

Android libgdx preferences not working(Android libgdx 首选项不起作用)

本文介绍了Android libgdx 首选项不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我必须改变什么才能使这段代码正常工作:

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();
    }
}

特别是不起作用的部分是每次在 android 设备上播放"都是错误的.在桌面上,第一次播放后是真的.

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.

推荐答案

我怀疑你被 static 状态在 Android 和桌面上的行为不同所困扰.当您在 Android 上退出应用程序时,您确定 VM 会退出吗?如果你在 Android 上快速重启你的应用,系统会为新的 Activity 重用同一个 Dalvik VM.由于 static boolean 已经初始化,初始化不会重新运行.如果您删除 static 以便在创建 GameData 时重新运行这些东西(可能它的实例没有存储在静态变量中),您应该走得更远.

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.

参见 http://bitiotic.com/博客/2013/05/23/libgdx-and-android-application-lifecycle/

这篇关于Android libgdx 首选项不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android libgdx 首选项不起作用

基础教程推荐