Its always good when an application remembers the last settings you've used. To add that functionality within your app, just add this class to your project.
01.
/**
02.
* This class is responsible for storing and loading the last used settings for the application.
03.
*
04.
* @author twig
05.
*/
06.
public
class
Settings {
07.
public
boolean
music =
true
;
08.
public
boolean
sfx =
true
;
09.
public
int
games_played =
0
;
10.
public
int
high_score =
0
;
11.
12.
public
Settings() {
13.
this
.loadSettings();
14.
}
15.
16.
public
void
loadSettings() {
17.
SharedPreferences settings = G.activity.getPreferences(Activity.MODE_PRIVATE);
18.
19.
this
.music = settings.getBoolean(
"music"
,
this
.music);
20.
this
.sfx = settings.getBoolean(
"sfx"
,
this
.sfx);
21.
this
.games_played = settings.getInt(
"games_played"
,
this
.games_played);
22.
this
.high_score = settings.getInt(
"high_score"
,
this
.high_score);
23.
}
24.
25.
public
void
saveSettings() {
26.
SharedPreferences settings = G.activity.getPreferences(Activity.MODE_PRIVATE);
27.
SharedPreferences.Editor editor = settings.edit();
28.
29.
editor.putBoolean(
"music"
,
this
.music);
30.
editor.putBoolean(
"sfx"
,
this
.sfx);
31.
editor.putInt(
"games_played"
,
this
.games_played);
32.
editor.putInt(
"high_score"
,
this
.high_score);
33.
34.
editor.commit();
35.
}
36.
}
When you create an instance of it, it'll automatically load up the old settings and the set defaults if it can't find it.
Remember, you'll still have to save when the application exits!
01.
/**
02.
* Activity is stopped by Android OS.
03.
*/
04.
@Override
05.
protected
void
onStop(){
06.
super
.onStop();
07.
08.
G.savePreferences();
09.
}