2

In my app, I am trying to save a key value as zero or one depending on whether a checkbox is checked. I retrieve these values in a different activity. However, when I attempt to retrieve the value, I am getting an empty string:

Saving key values in Activity 1(ScienceCoursesCheck):

public void setDefaults(String key, String value) {
   SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
   SharedPreferences.Editor editor = sharedPref.edit();
   editor.putString(key, value);
   editor.commit();

}
public void onCheckboxClicked(View view) {
   // Is the view now checked?
   boolean checked = ((CheckBox) view).isChecked();

   // Check which checkbox was clicked
   switch(view.getId()) {
       case R.id.checkBox_APBio:
           if (checked) {
               setDefaults("APBio", "1");
           }
           else {
               setDefaults("APBio", "0");
               break;
           }
}}

Retrieving key values in activity 2(MyHome):

public static String getDefaults(String key, Context context) {
   SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
   return preferences.getString(key, null);
}

public void myMethod() {
   SharedPreferences myPrefs = getSharedPreferences("ScienceCoursesCheck", MODE_PRIVATE);  
   String APBio = myPrefs.getString("APBio","");
   if (APBio.equals("1")){
       Button myButton = new Button(this);
       myButton.setText(APBio);
       RelativeLayout ll = (RelativeLayout)findViewById(R.id.activity_my_home);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
       ll.addView(myButton, lp);
   }
  }

Is the second activity accessing the correct SharedPreferences file?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Denizen_A
  • 41
  • 3

4 Answers4

0

The issue is because when you store the items in SharedPreferences you are using

PreferenceManager.getDefaultSharedPreferences

but when you retrieve them you are using

getSharedPreferences("ScienceCoursesCheck", MODE_PRIVATE);

These are two different SharedPreferences files. Try using getSharedPreferences("ScienceCoursesCheck", MODE_PRIVATE); for your setting method as well. Or perhaps you meant to use your getDefaults() method in activity 2 instead of myPrefs

Carl Poole
  • 1,970
  • 2
  • 23
  • 28
0

That is because you are saving in the DefaultSharedPreferences file

   SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

and retrieving from another file named "ScienceCoursesCheck"

SharedPreferences myPrefs = getSharedPreferences("ScienceCoursesCheck", MODE_PRIVATE);  

and they are not the same file!

See the difference between getSharedPreferences and getDefaultSharedPreferences in this answer

====

Solution is either to always use DefaultSharedPreferences OR always use the file named "ScienceCoursesCheck" .

Community
  • 1
  • 1
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

The problem is you're saving the values in the DefaultSharedPreference and trying to retrieve the values from the ScienceCoursesCheck preference file.

Try below code for setDefaults method.The getDefaultSharedPreference and getSharedPreference methods are different.

See this link for the difference.

 public void setDefaults(String key, String value) {
       SharedPreferences sharedPref = getApplicationContext().getDefaultSharedPreferences("ScienceCoursesCheck", MODE_PRIVATE);
       SharedPreferences.Editor editor = sharedPref.edit();
       editor.putString(key, value);
       editor.commit();

    }

Hope this helps you...

Community
  • 1
  • 1
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
0

Use the following code for sharedpreference. Call

Util.saveInfo(activityContext,_key,_value)

when you want to save the info. Then call

Util.getInfo(getApplicationContext(),_key)

to get the info.

public class Util{
public static void saveInfo(Context context, String key, String value )
{
    try
    {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).apply();
    }
    catch (Exception ex)
    {
        Log.e(TAG," saveInfo "+ex+"");
    }
}
public static String getInfo(Context context, String key)
{
    try {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(key,"");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return "";
}
Alvi
  • 767
  • 8
  • 20