应用程序终止后无法保存我的首选项
您好,我正在尝试在我的 Android 应用程序上实现一个设置页面。 我定义了一个 xml Preference 文件,在其中实现了 CheckBoxPreference 和 EditTextPreference。
运行应用程序时,所有设置都可以完美运行,但是当我杀死它时,我会丢失所有设置。
Preference.xml 文件:
<PreferenceCategory android:title="Connection">
<CheckBoxPreference
android:title="Auto Log-In"
android:summary="Auto connect "
android:key="autoLogIn"
android:enabled="true"
android:selectable="true"/>
<EditTextPreference
android:name="Server"
android:summary="Change the default server"
android:defaultValue="www.google.com"
android:title="Change server to:"
android:key="www.google.com" />
</PreferenceCategory>
Preferences.class
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);\
}
}
如何让应用保存设置?
更新: 我做了一些测试,在虚拟设备和我的带有 android 2.1 的旧 HTC Legend 上,设置工作正常!但它们不适用于 Android 2.2 的 Samsung Galaxy S!这对任何人来说都有意义吗?
Hi I'm trying to implement a settings page on my Android App.
I defined a xml Preference file, where I implemented CheckBoxPreference and EditTextPreference.
All the settings work perfectly while running the app, but when I kill it I lose all the settings.
Preference.xml file:
<PreferenceCategory android:title="Connection">
<CheckBoxPreference
android:title="Auto Log-In"
android:summary="Auto connect "
android:key="autoLogIn"
android:enabled="true"
android:selectable="true"/>
<EditTextPreference
android:name="Server"
android:summary="Change the default server"
android:defaultValue="www.google.com"
android:title="Change server to:"
android:key="www.google.com" />
</PreferenceCategory>
Preferences.class
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);\
}
}
How do I make the app save the settings?
UPDATE:
I did some test, and on the Virtual device and on my old HTC Legend with android 2.1 the settings work fine! But they don't work on the Samsung Galaxy S with android 2.2! Does this make sense to anybody?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过对我的三星 Galaxy S 进行软重置*解决了这个问题...不敢相信我花了一整天的时间来输入正确的代码。
希望这能为其他人节省一些时间。
Marco
*软重置:设置 ->隐私->出厂数据重置。
I solved the problem with a soft reset* of my Samsung Galaxy S... can't belive I waisted an all day on a correct code.
Hope this will save some time to others.
Marco
*Soft reset: Settings -> Privacy -> Factory data Reset.
不应该有任何理由不保存首选项。
当您执行更改(例如选中复选框)时,首选项会立即保存。
您确定您没有简单地将键与值混淆了吗?您的服务器密钥是
android:key="www.google.com"
也许这就是您认为它未保存的原因?There shouldn't be any reason for the prefs not to be saved.
The prefs are saved immediately when you perform a change (e.g. checking the checkbox).
Are you sure you didn't simply confuse the key with the value? Your server key is
android:key="www.google.com"
Maybe that's why you think it is not saved?我认为您需要为要存储在 SharedPreferences 中的首选项设置
android:persistent="true"
。如此处所示。I think you need to set
android:persistent="true"
for the preferences you want to store in SharedPreferences. As given here.