应用程序终止后无法保存我的首选项

发布于 2024-12-02 20:01:49 字数 1224 浏览 4 评论 0原文

您好,我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦一生花开无言 2024-12-09 20:01:49

我通过对我的三星 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.

浮生未歇 2024-12-09 20:01:49

不应该有任何理由不保存首选项。
当您执行更改(例如选中复选框)时,首选项会立即保存。

您确定您没有简单地将键与值混淆了吗?您的服务器密钥是 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?

神经暖 2024-12-09 20:01:49

我认为您需要为要存储在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文