From time to time, we need to persist some values. Typical examples are a high score or the settings of our game. AndEngine doesn't have any built-in mechanism for this, but we can use the Android SDK to achieve it.
There are two ways. A simple way is to use shared preferences, which is a key-value persistent storage. A more robust but more complicated way is to use the SQLite database, which is accessible from every Android application. We are going to use the simple way to store the high score and sound settings.
Each application has access to its
Preferences object. It's basically a key-value map. It is very simple to use preferences, but it's easy to make a mistake. First, we need to initialize the storage. This is done with the following code:
SharedPreferences settings = getSharedPreferences("andengine_game_prefs", MODE_PRIVATE);
We can have multiple preferences and they are identified by the name. This is the first parameter and any name will do. The second...