title: SharedPreferences not Automatically Saving?
body: <p>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 <code>ListPreference</code>) so I don't know why this isn't happening in my app.</p>
  
  <p>According to <a href="https://developer.android.com/guide/topics/ui/settings.html#Overview" rel="nofollow noreferrer">google</a> </p>
  
  <p><em>"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."</em></p>
  
  <p>Should I be manipulating a <code>SharedPreference.Editor</code> instance in my <code>OnSharedPreferenceChangeListener</code> or is there something else I need to do to make values of user settings persist?</p>
  
  <p><strong>The Problem in Code:</strong> 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. </p>
  
  <p>What do I need to do to make the values set by the user in my ListPreference persist?</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code> 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")); 
              ...
          }
      }
  </code></pre>
  
  <p><strong>Preferences Activity</strong></p>
  
  <pre><code>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? 
                  }
              }
          };
      }
      ...
  
  }
  </code></pre>
  
  <p><strong>Preferences.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      android:title="Settings"
      &lt;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" /&gt;
  &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p><strong>Strings.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;string name="app_name"&gt;MyApp&lt;/string&gt;
      &lt;string name="dummy_button"&gt;Dummy Button&lt;/string&gt;
      &lt;string name="dummy_content"&gt;DUMMY\nCONTENT&lt;/string&gt;
  
       &lt;!--Preference Menu Strings--&gt;
      &lt;string name="pref_wood"&gt;Wood Style&lt;/string&gt;
      &lt;string-array name="pref_wood_entries"&gt;
          &lt;item&gt;"Maple"&lt;/item&gt;
          &lt;item&gt;"Cedar"&lt;/item&gt;
          &lt;item&gt;"Oak"&lt;/item&gt;
      &lt;/string-array&gt;
      &lt;string-array name="pref_wood_values"&gt;
          &lt;item &gt;0&lt;/item&gt;
          &lt;item &gt;1&lt;/item&gt;
          &lt;item &gt;2&lt;/item&gt;
      &lt;/string-array&gt;
      &lt;string name="pref_wood_default"&gt;Maple&lt;/string&gt;
  &lt;/resources&gt;
  </code></pre>