Cody
Cody

Reputation: 1879

SharedPreferences not Automatically Saving?

My preference values aren't saving and everytime I read the SharedPreferences file it returns default values; changes are supposed to be automatically handled by the system when a user makes a choice (like selecting an option in a ListPreference) so I don't know why this isn't happening in my app.

According to google

"When the user changes a setting, the system updates the corresponding value in the SharedPreferences file for you. The only time you should directly interact with the associated SharedPreferences file is when you need to read the value in order to determine your app's behavior based on the user's setting."

Should I be manipulating a SharedPreference.Editor instance in my OnSharedPreferenceChangeListener or is there something else I need to do to make values of user settings persist?

The Problem in Code: As a result of this misunderstanding, my code doesn't persist user settings values (the default value is always chosen when I read the SharedPreference file in my MainActivity's OnCreate). As it is now, the buttons chosen in my Preferences menu View persist upon app restarts, but it seems that choosing an option in this menu doesn't save key values to the SharedPreferences file.

What do I need to do to make the values set by the user in my ListPreference persist?

MainActivity

 public class MainActivity extends FragmentActivity {
        private static int prefWoodColor;   //saved pref variable for OpenGL neck texture
        private SharedPreferences settings;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            //Restore preferences
            settings = PreferenceManager.getDefaultSharedPreferences(this);
            prefWoodColor = Integer.parseInt(settings.getString(this.KEY_PREF_WOOD_TYPE, "Maple")); 
            ...
        }
    }

Preferences Activity

public class FragmentSettingsMenu extends com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat {
    private SharedPreferences.OnSharedPreferenceChangeListener listener;

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

        // Load the preferences from the XML resource
        addPreferencesFromResource(R.xml.preferences);
        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                if (key.equals("pref_wood")) {
                    Preference woodPref = findPreference(key);
                    String color = woodPref.getSharedPreferences().getString(key, "Maple");
                    //Should I be calling edit.apply() logic here? 
                }
            }
        };
    }
    ...

}

Preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    android:title="Settings"
    <ListPreference
        android:key="pref_wood"
        android:title="@string/pref_wood"
        android:dialogTitle="@string/pref_wood"
        android:entries="@array/pref_wood_entries"
        android:entryValues="@array/pref_wood_values"
        android:defaultValue="@string/pref_wood_default" />
</PreferenceScreen>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApp</string>
    <string name="dummy_button">Dummy Button</string>
    <string name="dummy_content">DUMMY\nCONTENT</string>

     <!--Preference Menu Strings-->
    <string name="pref_wood">Wood Style</string>
    <string-array name="pref_wood_entries">
        <item>"Maple"</item>
        <item>"Cedar"</item>
        <item>"Oak"</item>
    </string-array>
    <string-array name="pref_wood_values">
        <item >0</item>
        <item >1</item>
        <item >2</item>
    </string-array>
    <string name="pref_wood_default">Maple</string>
</resources>

Upvotes: 0

Views: 1276

Answers (1)

The system saves changes automatically!

Upvotes: 1

Related Questions