0
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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);
spe.commit();

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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Note that SharedPreferences is Android-only. If you want cross-platform functionality, use LibGDX's Preferences class. \$\endgroup\$
    – Dashto
    Commented Jun 8, 2014 at 8:54
  • \$\begingroup\$ How do you use the SQlite DB's? \$\endgroup\$ Commented 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
    Commented Jun 8, 2014 at 22:39
  • \$\begingroup\$ OK thanks, I will look at both but thank you for the link. \$\endgroup\$ Commented 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 .