Alaa AbuZarifa
Alaa AbuZarifa

Reputation: 1249

Saving switch button state isn't working

I have a switch button for setting the sound of the app On and OFF, although it works normally but when I close the app then open it again.. the switch becomes false automatically..!? I've tried to save the last state via Shared Preference library Hawk , but it not clear to me how to do it right.!?

That's when I've tried to use Hawk

  // ....

   bool soundON = Hawk.get("sound"); // NPE !!!

  if (soundON) {
    soundSwitch.setChecked(true);
    soundSwitch.setSelected(true);
  } else {
    soundSwitch.setChecked(false);
    soundSwitch.setSelected(false);
  }


soundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      isSoundEnabled = true;
      Hawk.put("sound", true);
     } else {
      isSoundEnabled = false;
    }
    Log.i("is checked sound", isChecked + "");
    }});

UPDATE - Solution

I used @Shrikant answer, but with just a small change.! I used Boolean instead boolean so I can check for null values which Boolean allows you to do since it can be true or false or null unlike boolean which can be either true or false.

Here's the code

Boolean isSound = Hawk.get("sound");

if (isSound == null) {
  // I want the sound to be ON by default so I set it to true
  soundSwitch.setChecked(true);
  soundSwitch.setSelected(true);
} else {
  soundSwitch.setChecked(isSound);
  soundSwitch.setSelected(isSound);
  Log.i("Sound State > ", isSound + "");

}

Upvotes: 0

Views: 361

Answers (3)

yeyuzhou
yeyuzhou

Reputation: 11

You should save the check state at the time of the setOnCheckedChangeListener call. And get the check state at the time of the onCreate call.

Upvotes: 1

Shrikant
Shrikant

Reputation: 257

Try this

In onCreate() init that Hawk

Hawk.init(context).build();

after this get your sound value from Hawk and set it to switch

boolean isSound = Hawk.get("some",false);
soundSwitch.setChecked(isSound);
soundSwitch.setSelected(isSound);

there is not to check boolean by if else

soundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            Log.i("is checked sound", isChecked + "");
            Hawk.put("sound", isChecked);

        }
    });

I have used 2.0.1 version of Hawk.

Upvotes: 1

Junaid Fahad
Junaid Fahad

Reputation: 149

In onCreate() method you have to check isSoundEnabled from shared preference whether you checked it from your local variable.

try this:

bool soundValue = Hawk.get("sound"); 
 if (soundValue) {
    soundSwitch.setChecked(true);
    soundSwitch.setSelected(true);
  } else {
    soundSwitch.setChecked(false);
    soundSwitch.setSelected(false);
  }

instead of :

 if (isSoundEnabled) {
    soundSwitch.setChecked(true);
    soundSwitch.setSelected(true);
  } else {
    soundSwitch.setChecked(false);
    soundSwitch.setSelected(false);
  }

in onCreate()

Upvotes: 1

Related Questions