There is a lot different solutions for your problem. I am going to give you 2 solutions:
1 - Use the Application class. All the app could implement a Application class in this way:
public class ClassName extends Application {
}
It is a super class which from whatever Activity of the app, this class is accesible using this command:
getApplication();
Ok, you can read something in internet about how build this application class. The solution for your problem would be to create a variable in the application class like:
public class ClassName extends Application
{
private boolean isSoundEnabled;
public boolean getSoundEnabled() { return isSoundEnabled; }
public void setSoundEnabled(boolean isEnabled) { isSoundEnabled = isEnabled;}
@Override
public void onCreate() {
super.onCreate();
isSoundEnabled = true;
}
In this way, in your class Settings you can set this variable like:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(tbMusic.isChecked()) {
getApplication().setSoundEnabled ( true );
} else {
getApplication().setSoundEnabled ( false );
}
}
And everytime that you open your class Game you can check how this variable is setted :
getApplication().getSoundEnabled();
2 - The second solution for me is better because you save this information in memory, so everytime that the user open the app/game, the app can remember how the user setted in the pass this variable.
This is the way how we can remember flags, settings stuffs and recover this information after to close and open the app. We are going to use the SharedPreferences object. It is used to save values like int, float, boolean, etc (simple data objects) in the memory of the phone.
So in the Settings class, everytime that the user set the sound you can save the value:
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
if(tbMusic.isChecked()) {
prefs.edit().putBoolean ("IS_ENABLED_VARIABLE", true ).commit();
} else {
prefs.edit().putBoolean ("IS_ENABLED_VARIABLE", false ).commit();
}
And in you Game class you can recover this value using this code:
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
boolean isEnabled = prefs.getBoolean("IS_ENABLED_VARIABLE", true);
//The second parameter (true) is the value by default if the variable with the name IS_ENABLED_VARIABLE is not find for the app.
IS_ENABLED_VARIABLE --> It is the name of the variable, you need use the same to save or recover it. you can use whatever name that you wish.
com.example.app --> it is the name that you call to the sharedprefences area/directory into the memory of your phone. you can use the name that you wish.
Hope to be helpful and if it is, vote me, please!