简体   繁体   中英

Preferences are not persisted in android

I've read here , that Preferences are persisted automatically. I've tried to follow the instructions, but the checkbox always have its default value. My code goes as following:

1) I have a menu that opens Preferences screen:

int id = item.getItemId();
if (id == R.id.action_settings) {
          {
           Intent myIntent = new Intent(this, PreferencesActivity.class);
           startActivity(myIntent);
          }

2) Preferences Fragment class, that loads the settings:

public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preference_screen);
}

}

3) And activity, that loads the fragment:

public class PreferencesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}
}

Preference Screen xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:enabled="true" 
android:title="@string/ShowAdsTitle"  
android:summaryOn="@string/ShowAdsOnPreference" 
android:selectable="true" 
android:summaryOff="@string/ShowAdsOnPreference"
android:key="@string/value_showads_preference"/>
</PreferenceScreen>

Somehow my settings are not persistent. android:key is set up for each of my settings.

Thanks in advance!

It works for me so I am gonna describe my steps:

in MainActivity in onCreate:

Intent myIntent = new Intent(this, PrefActivity.class);
    startActivity(myIntent);

This will open the Preferences Activity.

public class PrefActivity extends Activity
{
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}
}

This will start the SettingsFragment.

@TargetApi(Build.VERSION_CODES.HONEYCOMB) public class SettingsFragment extends PreferenceFragment
{
@Override public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preference_screen);
}
}

And this is the code for the settings xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
    android:enabled="true"
    android:title="show ads"
    android:summaryOn="show ads on"
    android:selectable="true"
    android:summaryOff="show ads on"
    android:key="value"/>
</PreferenceScreen>

I am not sure what kind of class is the Preferences.class but it might be the problem. Anyway, this code works and it nicely checks and unchecks the preference and keeps it that way after going back form and to it :)

android:key="value"/>

尝试放置android:key的直接字符串而不是字符串中的值,因为您将无法访问已转换的键值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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