title: How can I save to shredpreferences if arraylist is null?
body: <p>Every time my Android app starts I get an <code>arraylist</code>, <code>MatchingArrayList</code>, from my server:</p>
  
  <pre><code>        try {
            JSONArray Object = new JSONArray(MatchingAsString);
            //for every object in the Array
            for (int x = 0; x &lt; Object.length(); x++) {
              final JSONObject obj = Object.getJSONObject(x);
              MatchingArrayList.add(obj.getString("usernameMatch"));
              }
  </code></pre>
  
  <p>It looks something like this: <code>[+111, +222]</code></p>
  
  <p>Then I save the <code>arraylist</code> to <code>sharedpreferences</code> like this:</p>
  
  <pre><code>            SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());
              SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();
              Gson gsonMAL = new Gson();
              String jsonMAL = gsonMAL.toJson(MatchingArrayList);
              editorMAL.putString("MatchingArrayList", jsonMAL);
              editorMAL.commit();
  </code></pre>
  
  <p>So the next time the app is used, if <code>MatchingArrayList</code> has changed to <code>[+111, +222, +333]</code>, for instance, it will overwrite the last used <code>arraylist</code> of <code>[+111, +222]</code></p>
  
  <p>It works well except if <code>MatchingArrayList</code> is <code>null</code> or is empty. The <code>sharedpreferences</code> don't update. I tried:</p>
  
  <pre><code>          if (MatchingArrayList == null) {
  
              SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());
              SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();
              editorMAL.putString("MatchingArrayList", null);
              editorMAL.commit();
            } else {
  
              //save MatchingArrayList into sharedpreferences so we can use it elsewhere
              SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());
              SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();
              Gson gsonMAL = new Gson();
              String jsonMAL = gsonMAL.toJson(MatchingArrayList);
              editorMAL.putString("MatchingArrayList", jsonMAL);
              editorMAL.commit();
  
            }
  
    But still the last arraylist is being used. How can I fix this?
  </code></pre>