I have sound on/off button in my app and I am running it with boolean. But problem is the user choice doesn't stay same. When the page refreshed, boolean value start over. And I want if user set sound on, even if he closed and open app again the sound should be on. I tried SharedPreferences but I couldn't make it work. I don't want to use SQL just for it. Do you have any suggestion?
private boolean soundCheck = false;
soundOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!soundCheck){
soundCheck=true;
v.setBackgroundResource(R.drawable.ic_soundOn);
}else {
soundCheck=false;
v.setBackgroundResource(R.drawable.ic_mute);
}
}
});
if(soundCheck){
playSound.start();
}else {
playSound(R.raw.silence);
}
what I tried with sharedPreferences
private boolean soundCheck;
final SharedPreferences GET = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor SET = GET.edit();
soundOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!soundCheck){
SET.putBoolean("soundCheck",true);
v.setBackgroundResource(R.drawable.ic_soundOn);
}else {
SET.putBoolean("soundCheck",false);
v.setBackgroundResource(R.drawable.ic_mute);
}
}
});