0

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);
                }
            }
        });
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jane S.
  • 65
  • 1
  • 7
  • 1
    `I tried SharedPreferences` add your code for shared preferences that you tried as you will probably end up with this solution – a_local_nobody May 20 '20 at 13:03
  • Whats the problem with SharedPreferences? SharedPreferences are the best way to go I guess. The documentation and Answers on Stackoverflow should guide and help you. https://developer.android.com/training/data-storage/shared-preferences – chrjs May 20 '20 at 13:04
  • Does this answer your question? [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Tyler V May 20 '20 at 13:17
  • I couldn't figure logic to how i should set boolean in on/off option. I am editting my question. – Jane S. May 20 '20 at 13:35
  • I did not see where u get the value that u have put in sharedpreference. u put it in onClickListener, but where u get it back? – Erwin May 20 '20 at 15:04
  • @Erwin I don't know where to get it, like i said previous comment, i couldn't figured the logic – Jane S. May 20 '20 at 16:06
  • As u said, u should put it here -> "When the page refreshed". And if u want this "if he closed and open app again the sound should be on", as far as I know u should use service – Erwin May 20 '20 at 16:40
  • sorry not service but sharedpreference should be working for what u want – Erwin May 20 '20 at 16:58

1 Answers1

0

@JaneS., after set value in shared preferences you have to apply those changes. Like:

SET.putBoolean("soundCheck",true).apply();
hata
  • 11,633
  • 6
  • 46
  • 69