I tried using this the answer from this question Need to save a high score for an Android game to save a simple int, score, but I'm having trouble. From my game activity I pass an int called newScore and have put it in my lose screen activity. I check if the newScore > highScore, and if so I put newScore as the new high score. I also display both. Currently it always shows the high score and score as the same on the screen. So even if I score 10, next time if I come back and score 1 it'll show 1 as the high score.
This is the code where I try and access the high score and put in the new score if it is larger
from `
public class LoseScreen extends AppCompatActivity`:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lose_screen);
Bundle b = getIntent().getExtras();
int newScore = b.getInt("newScore");
TextView textView6 = (TextView) findViewById(R.id.textView6);
textView6.setText("Score: " + newScore);
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int highScore = prefs.getInt("highScore", 0); //0 is the default value
if (newScore > highScore) {
prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("highScore", newScore);
editor.commit();
highScore = newScore;
}
TextView textView8 = (TextView) findViewById(R.id.textView8);
textView8.setText("High Score: " + highScore);
}
I'll also put in my quit method from my main game activity; I'm not posting the entire code for my class because I think it's rather large and irrelevant. If it'll help though I would be happy to post it.
public void quit() {
Intent intent = new Intent(ColorMatch2.this,
LoseScreen.class);
int newScore = score;
Bundle b = new Bundle();
b.putInt("newScore", newScore); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
}