Reputation: 699
I have a Preference activity with only one setting in it, Sound on or off, and it works fine in all my game EXCEPT in my main menu, where I can still hear button click sounds. But, after I exit the game and start it again, my sound is off. Code is exactly the same in all my activities, but somehow it does not work for the main activity until the restart of the game. What's that all about? Obviously the code is working but something is not right. Anyway, here's the code, not all, just the code related to this issue...only one button, for the exit...and below, just in case, i will post the entire activity code:
public boolean music;
MediaPlayer buttonClicks;
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
music = getPrefs.getBoolean("checkbox", true);
buttonClicks = MediaPlayer.create(this, R.raw.click);
dugme5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonBack.start();
}
finish();
}
});
Whole class:
public class Menu extends SwarmActivity implements OnClickListener{
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
MediaPlayer buttonClicks;
MediaPlayer buttonBack;
Button dugme1, dugme2, dugme3, dugme4, dugme5, dugme6;
TextView tvKviz;
public boolean music;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
music = getPrefs.getBoolean("checkbox", true);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
//if ( Swarm.isEnabled() ) {
Swarm.init(this, 4583, "e8398d93819da3d6d5f7d13f5b5a0deb");
//}
addListenerOnButton();
boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
if (!firstrun){
Intent swarm = new Intent("rs.androidaplikacijekvizopstekulture.SWARMPOPUP");
startActivity(swarm);
// Save the state
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
}
if ((!firstrun)&&(!isNetworkConnected())){
Intent netPopup = new Intent("rs.androidaplikacijekvizopstekulture.NETPOPUP");
startActivity(netPopup);
}
}
private void addListenerOnButton() {
Typeface naslov = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
buttonClicks = MediaPlayer.create(this, R.raw.click);
buttonBack = MediaPlayer.create(this, R.raw.button31);
tvKviz = (TextView) findViewById(R.id.tvKviz);
dugme1 = (Button) findViewById(R.id.bStart);
dugme2 = (Button) findViewById(R.id.bPravila);
dugme3 = (Button) findViewById(R.id.bTopScores);
dugme4 = (Button) findViewById(R.id.bPodesavanja);
dugme5 = (Button) findViewById(R.id.bIzlaz);
dugme6 = (Button) findViewById(R.id.bKontakt);
dugme1.setTypeface(dugmad);
dugme2.setTypeface(dugmad);
dugme3.setTypeface(dugmad);
dugme4.setTypeface(dugmad);
dugme5.setTypeface(dugmad);
dugme6.setTypeface(dugmad);
tvKviz.setTypeface(naslov);
dugme1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
startActivity(new Intent("rs.androidaplikacijekvizopstekulture.IZBOR"));
}
});
dugme2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
Intent pref = new Intent("rs.androidaplikacijekvizopstekulture.PRAVILA");
startActivity(pref);
}
}
});
dugme3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
Swarm.showLeaderboards();
}
});
dugme4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
Intent pref = new Intent("rs.androidaplikacijekvizopstekulture.PREFS");
startActivity(pref);
}
});
dugme5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonBack.start();
}
finish();
}
});
dugme6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
// email.putExtra(Intent.EXTRA_CC, new String[]{ to});
// email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, "subject...");
email.putExtra(Intent.EXTRA_TEXT, "poruka...");
// need this to prompts email client only
email.setType("message/rfc822");
try {
startActivity(Intent.createChooser(email, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Menu.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_contact:
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
// email.putExtra(Intent.EXTRA_CC, new String[]{ to});
// email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, "subject...");
email.putExtra(Intent.EXTRA_TEXT, "poruka...");
// need this to prompts email client only
email.setType("message/rfc822");
try {
startActivity(Intent.createChooser(email, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Menu.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Upvotes: 2
Views: 172
Reputation: 67209
You are getting the preference in your Activity's onCreate()
method. Unless you finish()
your main Activity somewhere, it will remain "created" until the Activity is finished for another reason or closed by the system.
If you get the preference and update your sound settings in onStart()
or onRestart()
instead, I think you will find it works as expected.
Upvotes: 1
Reputation: 22342
As far as I can see, you're only checking the preference when your activity is created, in onCreate()
. That's why it doesn't change until the game is restarted.
If you want it to check it on every button click, just write a simple function:
public boolean isMusicEnabled()
{
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
return getPrefs.getBoolean("checkbox", true);
}
Now, instead of checking if(music == true)
in each listener, you can use if(isMusicEnabled())
.
Alternatively, move the check to onResume()
, so it will be called when returning to your menu from the preference activity.
Upvotes: 1