1

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.

th3ramr0d
  • 484
  • 1
  • 7
  • 23
  • 2
    The first parameter in the `getSharedPreferences()` method is the name for the preference file you want to access. Make it something constant - e.g., `"weight_settings"` - instead of the changing value of the `weightSetting` variable. – Mike M. Oct 11 '15 at 09:14
  • can you add how you are initializing settingsPrefString inside onCreate – Shadow Droid Oct 11 '15 at 09:14
  • public SharedPreferences settingsPrefString; just above the onCreate method – th3ramr0d Oct 11 '15 at 09:19
  • not declaring...i am talking about initializing are you doing settingsPrefString = getSharedPreference(.. inside onCreate....If not then your settingsPrefString will be null.... – Shadow Droid Oct 11 '15 at 09:21

3 Answers3

2

Call these functions when you need them:

private void savePreferences(String key, String value){
    SharedPreferences sharedPreferences = getSharedPreferences("weightSetting", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }


  private String loadPreferences(String key){
    SharedPreferences sharedPreferences = getSharedPreferences("weightSetting", MODE_PRIVATE);
    String load = sharedPreferences.getString(key, "");
    return load;

   }
}

For example:

savePreferences("selected", "lb");

and

String s = loadPreferences("selected);
Patricia
  • 2,885
  • 2
  • 26
  • 32
1

The mistake you are doing is inside saveSettings method. While creating a shared preference for the first you are not assigning a constant name to it since you have used variable for name so change it. Create a global string constant for same

//SAVE WEIGHT SETTING BETWEEN LB/KG
settingsPrefString = getSharedPreferences(weightSetting, 0);
SharedPreferences.Editor editor = settingsPrefString.edit();

to

public static final String SETTINGS_PREFERENCE = "SETTINGS_PREFERENCE"
SharedPreference settingsPrefString

//your code
//saveSettings method
//SAVE WEIGHT SETTING BETWEEN LB/KG
settingsPrefString = getSharedPreferences(SETTINGS_PREFERENCE , Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settingsPrefString.edit();

if you look in the change then two constant are used which makes code more readable and accessable.

Also next time inside your onCreate() method you will have to add

settingsPrefString = getSharedPreferences(SETTINGS_PREFERENCE , Context.MODE_PRIVATE);

if(settingsPrefString != null)
{
    weightSetting = settingsPrefString.getString("weightSetting", "Couldn't load data!");
} else {
    System.out.println("settingsPrefString is NULL!");
}
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
  • @th3ramr0d welcome...alternatively you can go for the solution provided by WaldiDog...which you would enhance your code structure..but only problem is while saving and loading data...bcoz every time you would only save and load string data only so that needs to be handle properly – Shadow Droid Oct 11 '15 at 09:57
0

Replace

settingsPrefString = getSharedPreferences(weightSetting, 0);

With

settingsPrefString = getSharedPreferences(weightSetting, Context.MODE_PRIVATE);

This will also help

settingsPrefString = PreferenceManager.getDefaultSharedPreferences(this);

and follow this link

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
  • Context.MODE_PRIVATE is constant with value = 0 so directly passing 0 or using Context.MODE_PRIVATE is same you can refer: http://developer.android.com/reference/android/content/Context.html#MODE_PRIVATE – Shadow Droid Oct 11 '15 at 09:16
  • make `weightSetting` as fix. don't change its value. also did you try debugging your code. – Mohammad Tauqir Oct 11 '15 at 09:18