So I have a game that needs to save high scores for the number of waves, what guns the player has, positioning of the player and the zombies, and so on. I saw something about SharedPreferences but have no clue how to use them. Can someone point me to the right direction or willing to tell me how to use them correctly?


1 Answer 1


Here how to save your datas with SharedPreferences :

Store data

SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor spe = settings.edit();
spe.putInt("someKey", someIntegerValue);
spe.putFloat("anotherKey", someFloatValue);

Retrieve data

SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
float floatValue = sp.getFloat("anotherKey", defaultFloatValue);
int intValue = sp.getInt("someKey", defaultIntValue);

Where PREFS_NAME is a string containing the name you want for your preference file.

However, SharedPreference is not the only solution, you also can use Internal Storage and SQlite DBs.

  • 1
    \$\begingroup\$ Note that SharedPreferences is Android-only. If you want cross-platform functionality, use LibGDX's Preferences class. \$\endgroup\$
    – Dashto
    Jun 8, 2014 at 8:54
  • \$\begingroup\$ How do you use the SQlite DB's? \$\endgroup\$ Jun 8, 2014 at 15:27
  • \$\begingroup\$ You should loke there developer.android.com/guide/topics/data/data-storage.html#db. BTW, I think best solution is using your librairie functions as said Dashto. \$\endgroup\$
    – servabat
    Jun 8, 2014 at 22:39
  • \$\begingroup\$ OK thanks, I will look at both but thank you for the link. \$\endgroup\$ Jun 9, 2014 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .