【发布时间】:2014-04-06 16:25:58
【问题描述】:
我有一个 MainActivity 和一个从该 Activity 调用的 PreferenceActivity。我还有一个正在运行的服务来查询这些偏好。
当我打印这些值时。我明白了:
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
然后我打开 PreferenceActivity。这被打印出来了:
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
我把 pref_scrobble_percentage 改成 7,然后强制打印
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
我关闭 PreferenceActivity,然后强制打印:
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
我关闭 MainActivity,然后强制打印:
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
我杀死了应用程序,然后强制打印:
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
为什么当应用程序被终止而不是在我更改其值或关闭 PreferenceActivity 时保存首选项?
编辑好的,贴出相关代码。
查询首选项是这样完成的:
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());
}
}
我在 PreferenceActivity 上膨胀的 xml 是这样的:
<?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>
默认情况下会处理这些首选项。更改 CheckBoxPreferences 也不起作用。
【问题讨论】:
-
没有看到相关代码,我们应该如何猜测?
-
希望你现在有足够的相关代码:)
-
好的,点了;)。这很奇怪。 +1 以吸引更多关注!
-
所以,我一直在做我的研究。我的实际问题可以恢复为:我如何将我的 PreferenceActivity 首选项持久化到 APPLICATION 上下文而不是 Activity 上下文?
-
f***的正确拼写是F)@#。
标签: android preferenceactivity