简体   繁体   English

Android SharedPreferences / PreferenceFragment无效

[英]Android SharedPreferences/PreferenceFragment not working

I recently studied the section about handling application preferences with shared preferences and the PreferenceFragment in the Android Developer Documentation and made the following simple example: 我最近研究了有关使用共享首选项处理应用程序首选项以及Android Developer Documentation中PreferenceFragment ,并做了以下简单示例:

SettingsActivity.java SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(savedInstanceState == null)
            getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit()

    }

}

SettingsFragment.java SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

}

res/xml/settings.xml RES / XML / settings.xml中

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

    <PreferenceCategory android:title="@string/pref_general_category">    

        <EditTextPreference
            android:key="@id/pref_key_apiBaseUri"
            android:title="@string/pref_apiBaseUri_title"
            android:defaultValue="@string/pref_apiBaseUri_default"
            android:persistent="true"
            android:inputType="text"
            android:singleLine="true" />

    </PreferenceCategory>

</PreferenceScreen>

Other stuff: 其他的东西:

id        pref_key_apiBaseUri
string    pref_apiBaseUri_title       "Api Base Uri"
string    pref_api_baseUri_default    "http://acme.com/api/

Problem 问题

Straightforward stuff, eh? 直截了当的东西,嗯? I started the app, went to the preference menu / activity, the preference fragment got inflated and I clicked on " Api Base Uri ". 我启动了应用程序,转到首选项菜单/活动, 首选项片段被充气,我点击了“ Api Base Uri ”。 An EditText dialog popped up, I changed " http://acme.com/api " to " http://acme.com/api2 " and pressed on OK. 弹出一个EditText对话框,我将“ http://acme.com/api ”更改为“ http://acme.com/api2 ”并按下确定。 Now, as far as I understood the documentation, preferences should get saved automatically into the shared preferences . 现在,据我所知,文档中,首选项应自动保存到共享首选项中 When I reopen the EditText dialog, I see that my changes are stored. 当我重新打开EditText对话框时,我看到我的更改已存储。

HOWEVER.. when I close the activity (or the app) and go back to the settings, again, the default value is back, no changes have been made! 但是,当我关闭活动(或应用程序)并再次返回设置时,默认值又回来了,没有进行任何更改! What can I do to permanently save my settings? 如何永久保存我的设置?

In your settings.xml, you have: 在settings.xml中,您有:

<EditTextPreference
    android:key="@id/pref_key_apiBaseUri"
    android:title="@string/pref_apiBaseUri_title"
    android:defaultValue="@string/pref_apiBaseUri_default"
    android:persistent="true"
    android:inputType="text"
    android:singleLine="true" />

You must specify a string for key, as the docs for android:key say: 您必须为key指定一个字符串,因为android:key的文档说:

This attribute is required for preferences that persist a data value. 对于保留数据值的首选项,此属性是必需的。 It specifies the unique key (a string) the system uses when saving this setting's value in the SharedPreferences. 它指定在SharedPreferences中保存此设置值时系统使用的唯一键(字符串)。

It seems that your pref_key_apiBaseUri is an empty String. 看来你的pref_key_apiBaseUri是一个空字符串。 Change it to a valid String and move to strings.xml: 将其更改为有效的String并移至strings.xml:

<string name="pref_key_apiBaseUri">KEY</string>

then use as: 然后用作:

<EditTextPreference
    android:key="@string/pref_key_apiBaseUri"
    ...

OR keep it as it is, and just add + in front of id tag, which will create id which in turn will be used as key: 或者保持原样,只需在id标签前添加+ ,这将创建id,而id又用作键:

<EditTextPreference
    android:key="@+id/pref_key_apiBaseUri"
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM