-5

I saw this on stack Need to save a high score for an Android game

This is what it told me

//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();

//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value

I was wondering, should "key" be the string where the highscore is located? and does it matter what i name my prefs key. Thanks for your time.

Community
  • 1
  • 1
Igor Kronkdev
  • 67
  • 1
  • 10

2 Answers2

1

The Android Developers documentation is an excellent place to start with any question like this. SharedPreferences can be found here. SharedPreferences is a simple Key-Value pair storage, so if you put an int into your shared preferences as with the key "high score", then in the future, if you want to get that int, you need to use prefs.getInt("high score"). It doesn't matter what you name your keys, but it is generally good practice to use private static final string variables to store keys you will use on a regular basis.

Elli White
  • 1,440
  • 1
  • 12
  • 21
1

You can name the string whatever you like. Click here for documentation. You can name it anything from "foo" to "bar".