1

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);
}
Community
  • 1
  • 1
Johnny
  • 103
  • 1
  • 13
  • what have you tried to save you score? where is the problem? (there are basically three ways to store data in your app: a) sharedPreferences b)files c)databse ) if you know which approch you want let me know how you can be helped! (look at sharedPreferences!) – Martin Frank Oct 20 '16 at 03:36
  • I tried with sharedPreferences, it's in that first block called lose screen. It's this part: 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; } – Johnny Oct 20 '16 at 03:43
  • It's hard to read in the comment here but it's about 20 lines down in the first block, starting with Shared Preferences prefs = – Johnny Oct 20 '16 at 03:45
  • uhm right, i didn't see that! sorry, my mistake! – Martin Frank Oct 20 '16 at 03:53
  • That's alright, it's easy to miss. Can you tell what I'm doing wrong? I'm sure it's a simple mistake but I'm an android noob and am not sure why it isn't saving the high score properly – Johnny Oct 20 '16 at 04:08
  • i'm not sure, looks good so far... now i'm curious as well... – Martin Frank Oct 20 '16 at 04:15
  • Well turns out the code worked all along. I had to rebuild and clean my project for an unrelated error and now the high score is properly saving too – Johnny Oct 20 '16 at 05:28

1 Answers1

0

So this is pretty strange, but without changing the code it is now working. After posting this question my android studio started acting up; I got a cannot resolve symbol 'r' error and had to rebuild and clean my project. After a few restarts I noticed that high score was being saved properly too now. Weird.

Johnny
  • 103
  • 1
  • 13