How to reset a variable <p>I'm not sure if I'm missing something obvious or whether this is actually something I don't know how to do, but I'd like to be able to reset some variables (<code>integers</code>). I have a method that increments them, and they're saved in <code>SharedPreferences</code>. I'd like to be able to reset these to 0 on a button click.</p> <p>I can clear <code>SharedPreferences</code> using the clear(); command, but the ints stay the same so as soon as they are called again they are back to what they were before the reset. I've also tried setting them equal to 0, but then I can't increment them any more. Is there a way to just reset or delete variables?</p> <p>Or, if that's not possible/too difficult, is there another way I can completely reset these ints?</p> <p>Thanks</p> <p>This is part of the incrementing code. This works. I can also clear <code>SharedPreferences</code>; that part is fine too. <em>The only part that doesn't work is resetting the int to 0, but keeping it incrementable</em>.</p> <pre><code>Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton)); PlusOneButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { intName++; }}); </code></pre> <p>EDIT: the final code that works</p> <pre><code>import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ThirdScreenActivity extends Activity { public int myIntPlusOne; public static final String PREFS = "MyPrefsFile"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen2); SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("PREFS", 0); myIntPlusOne = sharedPrefs.getInt("myIntPlusOneSaved", 0); if ("ListView Item 1".equals(position)) { myIntPlusOne = sharedPrefs.getInt("myIntPlusOneSaved", 0); TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview)); PlusOne.setText(String.valueOf(myIntPlusOne)); Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton)); PlusOneButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myIntPlusOne++; TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview)); PlusOne.setText((myIntPlusOne)+""); SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("PREFS", 0); Editor editor = sharedPrefs.edit(); editor.putInt("myIntPlusOneSaved", myIntPlusOne); editor.commit(); } }); } else if... } Button btnClear = (Button) findViewById(R.id.clearbtn); btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myIntPlusOne=0; TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview)); PlusOne.setText("0"); } }); } public void onPause() { super.onPause(); SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("PREFS", 0); Editor editor = sharedPrefs.edit(); editor.putInt("myIntPlusOneSaved", myIntPlusOne); editor.commit(); } } </code></pre>