title: SharedPreferences not incrementing integer value - Android
body: <p>I am wanting to store an int in <code>SharedPreferences</code>.  The int represents which level the user is on in my game.  Before the game starts, this code is executed:</p>
  
  <pre><code>    int level;
  
      // Restore preferences
      SharedPreferences settings = getSharedPreferences("level_SP", 0);
      level = settings.getInt("currentLevel", 0);
      if(level == 0) {
          level++;
      }
      String str = Integer.toString(level);
      title.setText(str); 
  </code></pre>
  
  <p>The text that is outputted from <code>title.setText(str);</code> is <code>1</code>.</p>
  
  <p>For testing purposes, no matter how the user does in the game he passes the level.  After the game is over, this code is executed.</p>
  
  <pre><code>public void levels() {
  
      // do calculations here to see if user passed level.
  
  
      // Restore preferences
      SharedPreferences settings = getSharedPreferences("level_SP", 0);
      int level = settings.getInt("currentLevel", 0);
      int newLevel = level++;
  
      // We need an Editor object to make preference changes.
      SharedPreferences.Editor editor = settings.edit();
      editor.putInt("currentLevel", newLevel);
  
      // Commit the edits!
      editor.commit();
  }
  </code></pre>
  
  <p>Then when the user starts the game again, they should be at level 2.  In the code pasted at the top of this post, this line of code:  <code>title.setText(str);</code> should output <code>2</code>, but it still is saying <code>1</code>.  That tells me that the int I stored in <code>SharedPreferences</code> is not incrementing.</p>
  
  <p>My question is, am I "getting" the <code>SharedPreferences</code> correctly?  If yes, am I "editting" the <code>SharedPreferences</code> correctly?  If not on either one, what am I doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thought:  Do the <code>0</code> that are scattered throughout my above code mean anything?  Should they be something else?</p>