I'm trying to save a high score using SharedPreferences
. I initialize currentScore
and highestScore
to zero at the very beginning of my main activity class.
public class MainActivity extends AppCompatActivity {
int currentScore = 0;
int highestScore = 0;
...
}
When I click on a button to begin a new run after a run has ended, currentScore
replaces highestScore
if it's higher. This part works well. However, I don't know where to create and use SharedPreferences
, editor
, putInt
, and commit
. Each time I close the app, highestScore
is reset to zero.
public void newRun(View view){
if(currentScore > highestScore){
highestScore = currentScore;
}
...
//Store and commit highestScore here ?
//Will it be reset since it is initialized to zero at the class beginning?
}