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.