Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LibGDX with Android Preferences

I'm trying to use Andrioid's preferences system in conjunction with LibGDX's preferences system. They both use SharedPreferences as a backend, so I figure they should be able to work together, but when I try to load the data in LibGDX's preferences, I don't get any data back.

My Android preferences.xml file (I know it's short, it'll have much more later :P):

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <EditTextPreference 
        android:key="framerate"
        android:title="Set Framerate"
        android:enabled="true"
        android:persistent="true"
        android:defaultValue="25" />
</PreferenceScreen>

Here is my PreferenceActivity:

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class WallpaperSettings extends PreferenceActivity {

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT < 11) {
            addPreferencesFromResource(R.xml.preferences);
        } else {
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
        }

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
}

When I call it from a subclass of com.badlogic.gdx.Game, I use

Preferences pref = Gdx.app.getPreferences("preferences");
pref.getInteger("framerate");

The number of keys within pref is 0.

Anyone have a clue as to how this might be fixed?

like image 942
kotakotakota Avatar asked Apr 12 '26 09:04

kotakotakota


1 Answers

Thanks to http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=6365#p32981 I was able to solve the problem.

Just a note, the code works for both Android 2.x and 3.0+.

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class WallpaperSettings extends PreferenceActivity {

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT < 11) {
            addPreferencesFromResource(R.xml.preferences);
        } else {
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
        }

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            getPreferenceManager().setSharedPreferencesName("preferences");
            getPreferenceManager().setSharedPreferencesMode(0);
        }
    }
}
like image 58
kotakotakota Avatar answered Apr 14 '26 22:04

kotakotakota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!