I have a radiogroup with two radio buttons. I want to select one radio button and hit save so that when I return the same radiobutton will be selected already. The problem is that even after I hit save the sharedpreferences in onCreate always returns null. I know that the save function is executing because the toast runs and I get the System.out.println printed in my console.
public void saveSettings(View view)
{
Toast.makeText(getApplicationContext(), "Settings Saved", Toast.LENGTH_LONG).show();
//DETERMINE WHICH RADIO BUTTON IS SELECTED
RadioButton lbSetting = (RadioButton)findViewById(R.id.weightSettingLB);
RadioButton kgSetting = (RadioButton)findViewById(R.id.weightSettingKG);
if(lbSetting.isChecked())
{
weightSetting = "lb";
}
if(kgSetting.isChecked())
{
weightSetting = "kg";
}
System.out.println("The " + weightSetting + " radio button has been selected.");
//SAVE WEIGHT SETTING BETWEEN LB/KG
settingsPrefString = getSharedPreferences(weightSetting, 0);
SharedPreferences.Editor editor = settingsPrefString.edit();
editor.putString("weightSetting", weightSetting);
editor.commit();
}
In my onCreate I try to retrieve the saved data as
if(settingsPrefString != null)
{
weightSetting = settingsPrefString.getString("weightSetting", "Couldn't load data!");
}
if(settingsPrefString == null)
{
System.out.println("settingsPrefString is NULL!");
}
Even after I hit save the sharedpreferences is always being returned as null.