Search code examples
androidsharedpreferencesuiswitch

How can I save the state of my Switch using SharedPreferences?


I have two Switches implemented where only one can be True at a time, and now i am trying to save the state of the switch. I've looked at other StackOverflow questions similar to mine, but something is not working. Here is my code:

public class StartingActivity extends AppCompatActivity
{

private Switch hourly, salary;
private Boolean hrlySwitch, slrySwitch;
private Double hrly, slry, tax;
private SharedPreferences pref;

private static  String TAG = "tag";

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting);

    pref = getApplicationContext().getSharedPreferences("switchInfo", MODE_PRIVATE);

    hourly = (Switch)findViewById(R.id.hourly);
    salary = (Switch)findViewById(R.id.salary);

    //get Bool SharedPreference
    hourly.setChecked(pref.getBoolean("hrlyBool", false));
    salary.setChecked(pref.getBoolean("slryBool", true));

    //switching
    salary.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (salary.isChecked())
            {
                hourly.setChecked(false);
                slrySwitch = true;
                hrlySwitch = false;
            }
            else
            {
                hourly.setChecked(true);
                slrySwitch = false;
                hrlySwitch = true;
            }
        }
    });

    hourly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (hourly.isChecked())
            {
                salary.setChecked(false);
                slrySwitch = false;
                hrlySwitch = true;
            }
            else
            {
                salary.setChecked(true);
                slrySwitch = true;
                hrlySwitch = false;
            }
        }
    });

    Log.d(TAG,"before switch rule");
    //put switch rule
    SharedPreferences.Editor editor = pref.edit();
    Log.d(TAG,"pref.edit()");
    editor.putBoolean("hrlyBool",hrlySwitch);
    Log.d(TAG,"putBool hourly");
    editor.putBoolean("slryBool", slrySwitch);
    Log.d(TAG,"putBool salary");
    editor.commit();
    Log.d(TAG,"after switch rule");

}

} 

I included Log statements to track where my app crashes, and the last line of code executed is the "pref.edit()" Log statement. But i'm not sure why my putBoolean could be causing an issue.


Solution

  • Can't you just do this?

    public class StartingActivity extends AppCompatActivity
    {   
        private static final String SWITCH_PREFS = "switchInfo";
        private static final String HOURLY_PREF = "hrlyBool";
        private static final String SALARY_PREF = "slryBool";
    
        private Switch hourly, salary;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_starting);
    
            hourly = (Switch)findViewById(R.id.hourly);
            salary = (Switch)findViewById(R.id.salary);
    
            hourly.setChecked(getApplicationContext()
                    .getSharedPreferences(StartingActivity.SWITCH_PREFS, MODE_PRIVATE)
                    .getBoolean(StartingActivity.HOURLY_PREF, true));
            salary.setChecked(getApplicationContext()
                    .getSharedPreferences(StartingActivity.SWITCH_PREFS, MODE_PRIVATE)
                    .getBoolean(StartingActivity.SALARY_PREF, false));
    
            salary.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    hourly.setChecked(!isChecked);
                    saveSwitchStates();
                }
            });
    
            hourly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    salary.setChecked(!isChecked);
                    saveSwitchStates();
                }
            });
        }
    
        private void saveSwitchStates() {
            SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(StartingActivity.SWITCH_PREFS,
                    MODE_PRIVATE).edit();
            editor.putBoolean(StartingActivity.HOURLY_PREF, hourly.isChecked());
            editor.putBoolean(StartingActivity.SALARY_PREF, salary.isChecked());
            editor.commit();
        }
    } 
    

    I am not sure why you are saving all those values in the StartingActivity class, just use the switches isChecked function when you want to know if it is checked, and the listeners just need to swap the others value.