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
\$\begingroup\$
\$\endgroup\$
4
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.
-
1\$\begingroup\$ Note that SharedPreferences is Android-only. If you want cross-platform functionality, use LibGDX's Preferences class. \$\endgroup\$– DashtoCommented 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\$– servabatCommented 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