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 首选项不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)