Charlie-Blake
Charlie-Blake

Reputation: 11050

PreferenceActivity saving preferences when app is destroyed

I have a MainActivity and a PreferenceActivity that is called from that Activity. I also have a Service running that queries for those preferences.

When I print those values. I get this:

D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50

Then I open PreferenceActivity. This gets printed:

D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50

I change pref_scrobble_percentage to 7, then force the printing

D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50

I close the PreferenceActivity, then force printing:

D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50

I close the MainActivity, then force printing:

D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50

I kill the app, then force printing:

D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 7

Why are the preferences getting saved when the app is killed instead of when I change their values or close the PreferenceActivity?

EDIT Ok, posting relevant code.

Querying the prefs is done like this:

public static boolean getScrobbleEnabled(final Context ctx) {
        final String key = ctx.getString(R.string.pref_scrobble);
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx);
        printPrefs(settings);
        return settings.getBoolean(key, true);
    }

private static void printPrefs(final SharedPreferences settings) {
    Map<String, ?> map = settings.getAll();
    for (String str : map.keySet()) {
        Log.d(str, map.get(str).toString());
    }
}

The xml which I inflate on the PreferenceActivity is this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="@string/scrobbler_conf" >
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="@string/pref_scrobble"
            android:summary="@string/enable_scrobbling_subtitle"
            android:title="@string/enable_scrobbling" />
        <CheckBoxPreference
            android:defaultValue="true"
            android:dependency="@string/pref_scrobble"
            android:key="@string/pref_show_notification"
            android:summary="@string/show_notification_subtitle"
            android:title="@string/show_notification" />

        <com.garli.lastfm.controller.preferences.SeekBarPreference
            android:defaultValue="50"
            android:dependency="@string/pref_scrobble"
            android:key="@string/pref_scrobble_percentage"
            android:max="100"
            android:summary="@string/scrobble_percentage_subtitle"
            android:title="@string/scrobble_percentage" />

        <CheckBoxPreference
            android:defaultValue="false"
            android:dependency="@string/pref_scrobble"
            android:key="@string/pref_scrobble_only_on_wifi"
            android:summary="@string/scrobble_only_on_wifi_subtitle"
            android:title="@string/scrobble_only_on_wifi" />
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/ads" >
        <Preference
            android:defaultValue="false"
            android:key="@string/pref_remove_ads"
            android:summary="@string/ads_subtitle"
            android:title="@string/ads" />
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/about" >
        <Preference
            android:key="pref_version"
            android:title="@string/version" />
    </PreferenceCategory>

</PreferenceScreen>

Those preferences are handled by default. Changing CheckBoxPreferences doesn't work either.

Upvotes: 2

Views: 567

Answers (2)

Steve M
Steve M

Reputation: 9784

I've had this issue with a Service not seeing the preferences before, I can't remember the details but returning the SharedPreferences like this resolved it. I remember thinking it seemed odd since the Service wasn't actually in a separate process, but it fixed the problem. The documentation seems a little confusing on this flag.

public static SharedPreferences getPref(Context context)
{
    int mode = android.os.Build.VERSION.SDK_INT >= 11 ? 
                   Context.MODE_MULTI_PROCESS : 
                   Context.MODE_PRIVATE;
    return context.getSharedPreferences(SHARED_PREF_NAME, mode);
}

Also, are you inflating the XML the proper way in onCreate()?

addPreferencesFromResource(R.xml.preferences);

Check out this for changing the default shared preferences name and mode.

public class MyPreferencesActivity extends PreferenceActivity {
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         PreferenceManager prefMgr = getPreferenceManager();
         prefMgr.setSharedPreferencesName("my_preferences");
         prefMgr.setSharedPreferencesMode(MODE_MULTI_PROCESS);

         addPreferencesFromResource(R.xml.preferences);
    }
}

Let us know if you solve the problem.

Upvotes: 1

danny117
danny117

Reputation: 5651

I use these static methods to save integer prefs for the application. This works with all my apps because I use a common R.string.app_name and activity which is a context to identify my preferences file.

I call it like this.

int x = GoPreferences.getInt(getActivity(),MAP_TYPE,GoogleMap.MAP_TYPE_NORMAL);

...

GoPreferences.putInt(getActivity(),MAP_TYPE, x);

...

//simple static preferences methods  
public class GoPreferences {

public static void putInt(Context context, String s, int b) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            context.getString(R.string.app_name), Context.MODE_PRIVATE);
    sharedPref.edit().putInt(s, b).commit();
}

public static int getInt(Context context, String s, int defaultvalue) {
    defaultvalue = context.getSharedPreferences(context.getString(R.string.app_name),
            Context.MODE_PRIVATE).getInt(s, defaultvalue);
    return defaultvalue;
}
}

Good Luck

Danny117

Upvotes: 0

Related Questions