Preferences in libgdx

Yeah, you can use that now. Nighlies are building. To fetch a Preferences instance do this:

Preferences prefs = Gdx.app.getPreferences("my-preferences");

Note that the name must be useable as a file name. On Android we wrap SharedPreferences. On the desktop we write a Properties XML file to user/preferences-name (“my-preferences” above). That’s not perfect and it’s why we didn’t want to implement that in the first place (Java Preferences are not a solution either…).

Once you have that you can put and get booleans, ints, longs, floats and strings (will be stored as UTF-8).

prefs.putBoolean("bool", true);
prefs.putInteger("int", 1234);
prefs.putLong("long", Long.MAX_VALUE);
prefs.putFloat("float", 1.2345f);
prefs.putString("string", "test!");
		
if(prefs.getBoolean("bool") != true) throw new GdxRuntimeException("bool failed");
if(prefs.getInteger("int") != 1234) throw new GdxRuntimeException("int failed");
if(prefs.getLong("long") != Long.MAX_VALUE) throw new GdxRuntimeException("long failed");
if(prefs.getFloat("float") != 1.2345f) throw new GdxRuntimeException("float failed");
if(!prefs.getString("string").equals("test!")) throw new GdxRuntimeException("string failed");		

To make sure your changes are actually persisted you can use

prefs.flush();

That’s really only relevant on the desktop. If your app terminates due to a severe error the mechanism persisting the thing might not get invoked (no its not a finalizer, but something similar). Flush will immediately persist the current preferences state. So its good practice to call this after you changed your preferences.

That’s it. Enjoy.

12 thoughts on “Preferences in libgdx

  1. Hooray! :D

    I’ve been waiting on this to implement my options menu :)

    Though it is I suppose, less than ideal to save to the user dir on PCs, but good enough IMO.

  2. the problem on the PC is that the app could be run as a webstart or applet. in which case we don’t have the notion of an app relative directory. It should work in 90% of the cases though. Open for suggestions!

  3. hi, why not adding some support to define a category, or name or whatever than on the pc was used as the app directory, so you can store the preferences as user/appname/my-preferences

    p.s. libgdx rocks

  4. Hi!
    I’m having a bit of trouble with this.

    On the deskrop everything works nicely, but when trying it on my phone it doesn’t seem to save. What might be wrong? I’m flushing it after changing preferences.

  5. I have the same issue. everything is fine on pc, but it can not save on the android device. Where exatcly should I look for the preference file?

  6. Same problem on Nexus 7. Even after flushing, setting “true” to a specific boolean key leaves that specific boolean key as “false”. Switching to local file for preference serialization instead.

  7. Nevermind. The error was that you can get only ONE instance of the Preferences, possibly due to the wrapper sending the Application as a parameter for the SharedPreferences. I guess I was the one at fault!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>