title: Strange unexplained behaviour in SharedPreferences Android
body: <p>I'm not new to <code>sharedPreferences</code>, and I decided to use them to store the titles of courses in which users create, and then are loaded into a listView. The course titles are stored in a set which is then put into the <code>sharedPreference</code>. The issue here is when i input the first course(as a user), it gets saved persistently, but adding more courses also saves as well, and works while the app is active, but on app restart, the sharedPreference returns back to the initial value of only one item in the set. </p>
  
  <p>Naturally, i looked up the issue, and was able to solve it by using <code>editor.clear()</code> which i got from this reference <a href="https://looksok.wordpress.com/2012/09/21/sharedpreferences-not-saved-after-app-restart/" rel="nofollow">https://looksok.wordpress.com/2012/09/21/sharedpreferences-not-saved-after-app-restart/</a></p>
  
  <p>Notice that the solution was described as "the tricky one". I really want to know what this line meant. I've checked on the issue, but still can't understand why this case is so different, and such abnormalities scare me whenever i use those classes. So here's my code: </p>
  
  <pre><code>    // Shared Preferences
      SharedPreferences customPrefs;
  
      // Editor for Shared preferences
      SharedPreferences.Editor editor;
  
      private CustomCourseDBHelper customDBHelper;
  
      private final static int ADD_CUSTOM_COURSE_REQUEST = 1;
      private final static int EDIT_CUSTOM_COURSE_REQUEST = 2;
      private final static String TITLE_PREFS = "customCourseTitles";
      private final static String TITLE_PREF_LIST = "customCourseList";
  
      private ListView cListView;
      private Set&lt;String&gt; allTitles = new LinkedHashSet&lt;&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.custom_course_list);
  
          mToolbar = (Toolbar) findViewById(R.id.tool_bar_shadow);
          cListView = (ListView) findViewById(R.id.customQuestionList);
          addButton = (FloatingActionButton) findViewById(R.id.addButton);
  
          cAdapter = new CustomCourseAdapter(this);
  
          setSupportActionBar(mToolbar);
  
          cListView.setAdapter(cAdapter);
  
          //declare shared prefs
          customPrefs = getSharedPreferences(TITLE_PREFS, MODE_PRIVATE);
  
          //set on long press options
          optionsList.add("Delete Course");
  
          //set icon for add button
          addButton.setImageDrawable(getResources()
                  .getDrawable(R.drawable.ic_add_white_24dp));
  
          addButton.setOnClickListener(new OnClickListener(){
  
              @Override
              public void onClick(View v) {
                  Intent i = new Intent(CustomCourseList.this, AddCustomCourseActivity.class);
                  startActivityForResult(i, ADD_CUSTOM_COURSE_REQUEST);
              }
  
          });
  }
  </code></pre>
  
  <p>And here's where the saving occurs</p>
  
  <pre><code>        @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data){
              if (resultCode == RESULT_OK &amp;&amp; requestCode == ADD_CUSTOM_COURSE_REQUEST){
                  CustomCourseItem cci = new CustomCourseItem(data);
                  cAdapter.add(cci);
  
                  //save the provided details in a database
                  customDBHelper = new CustomCourseDBHelper(this, cci.getPath());
                  customDBHelper.addCustomCourse(cci);
  
                  editor = customPrefs.edit();
  
                  Set&lt;String&gt; mSet = customPrefs.getStringSet(TITLE_PREF_LIST, null);
  
                  if (mSet != null)
                      allTitles = mSet;
  
                  //store the given database names in a set in a shared preference
                  allTitles.add(cci.getPath());
                  editor.clear(); //this line here, without it, the sharedPref is not persistent, 
                  //i didn't see any clear reason as to why this occurs, when i read the documentary
                  editor.putStringSet(TITLE_PREF_LIST, allTitles);
                  editor.commit();
              }
  
  
              //listen request for edits made to courses
              if (resultCode == RESULT_OK &amp;&amp; requestCode == EDIT_CUSTOM_COURSE_REQUEST){
                  if (data.getBooleanExtra("edited", false)){
                      restructureDbOnEdit();
                  }
              }
  
          }
  </code></pre>
  
  <p>PS. i omitted some unnecessary code like UI implementations and other non-sharedPreference related stuff.
  So even though the app runs well now, any further insight on this issue would be really welcomed.</p>