Game Development Reference
In-Depth Information
Persisting data
If you want your data to persist after exit, you should use the Preferences class. It
is merely a dictionary or a hash map data type that stores multiple key-value pairs
in a file. LibGDX will create a new preferences file on the fly if it does not exist. You
can have several preference files using unique names in order to split up data into
categories. To get access to a preference file, you need to request a Preferences
instance by its filename as follows:
Preferences prefs = Gdx.app.getPreferences("settings.prefs");
To write a (new) value, you have to choose a key under which the value should be
stored. If this key already exists in a preferences file, it will be overwritten. Do not
forget to call flush() afterwards, as shown in the following code, to persist the
data, or else all the changes will be lost:
prefs.putInteger("sound_volume", 100); // volume @ 100%
prefs.flush();
Persisting data needs a lot more time than just modifying values in
memory (without flushing). Therefore, it is always better to modify as
many values as possible before a final flush() method is executed.
To read back a certain value from the preferences file, you need to know the
corresponding key. If this key does not exist, it will be set to the default value.
You can optionally pass your own default value as the second argument (for
example, in the following listing, 50 is the default sound volume):
int soundVolume = prefs.getInteger("sound_volume", 50);
Querying the Android API level
On Android, you can query the Android API level that allows you to handle things
differently for certain versions of the Android OS. Use the following listing to find
out the version:
Gdx.app.getVersion();
On platforms other than Android, the version returned is always 0 .
 
Search WWH ::




Custom Search