-3

I'm very new to Android Developing. I made a small 2d game and I was wondering what the easiest way to store the high-score would be, so if the app is exited and returned to the high-score is retrieved from the last session. Any help would be appreciated!

Paymon Wang-Lotfi
  • 545
  • 2
  • 11
  • 29
  • 1
    Also see [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – hichris123 Feb 27 '15 at 02:57

1 Answers1

7

For a simple piece of data such as high score you could easily save this in SharedPreferences. This is a simple key value pairing that persists across app launches and doesn't require a SQL database.

To obtain a shared preference:

SharedPreferences prefs = this.getSharedPreferences( "com.example.app", Context.MODE_PRIVATE);

To edit and save to shared preference:

int highScore = 100; prefs.edit().putInt("HighScore", highScore).apply();

To retrieve shared preference :

int score = prefs.getInt("HighScore", 0);
Rob212
  • 315
  • 2
  • 13