Change Android Switch State

user2677095 picture user2677095 · Sep 20, 2014 · Viewed 42.6k times · Source

I currently am developing an app that has a menu, and one of the options on the menu is "Settings" where the user can basically decide to turn off sounds and other things like that. I currently have two switches in the Settings activity. Here is the java code for the Settings activity so far:

import android.support.v7.app.ActionBarActivity;


public class Options extends ActionBarActivity {
private  Switch ding;
private Switch countdown;
public  boolean isDingChecked;
public  boolean isCountdownChecked;
public static final String PREFS = "examplePrefs";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
ding = (Switch) findViewById(R.id.switch1);
ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
        Editor editor = examplePrefs.edit();
        editor.putBoolean("userMessage", isChecked);
        editor.commit();

        //System.out.println(examplePrefs.getBoolean("userMessage", isChecked));
        isDingChecked = examplePrefs.getBoolean("userMessage", isChecked);
        System.out.println(isDingChecked + " is ding checked");
        ding.setChecked(isDingChecked);
    }
});

countdown = (Switch) findViewById(R.id.switch2);
countdown.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // do something, the isChecked will be
        // true if the switch is in the On position
        isCountdownChecked = isChecked;

    }
});     
 }
 }

I am able to use the boolean values in my other activity, so the SharedPreference works fine. However, when I go back to my menu activity and go back to this options activity, the state of the switches goes back to its default values of being true, regardless of what the user states. Is there anyway I can fix this?

ding.setChecked(isDingChecked)

Isn't really doing anything I guess. I know I posted a question similar to this in the past, it's just that one hasn't had much activity and I've been working on this issue for quite some time. Thanks!

Answer

Will Thomson picture Will Thomson · Sep 20, 2014

Try something like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);
    ding = (Switch) findViewById(R.id.switch1);

    //grab prefs first
    final SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
    final Editor editor = examplePrefs.edit();
    ding.setChecked(examplePrefs.getBoolean("your_key", false)); //false default 


    ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //commit prefs on change
            editor.putBoolean("your_key", isChecked);
            editor.commit();

            System.out.println(isDingChecked + " is ding checked");
        }
    });