Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Activity Lifecycle Introducing SharedPreferences Saving and Retrieving Data with SharedPreferences

michael lee
michael lee
5,179 Points

how to set the string value to the check?

in my code I am trying to convert String into check value in order to retrieve, but can't find a way to do that

MainActivity.java
import android.view.View;
import android.os.Bundle;

public class MainActivity extends Activity {

  public CheckBox mCheckBox;
   static final String PREFS_FILE = "com.teamtreehouse.sharedpreferencesapp.preferences";
   static final String KEY_CHECKBOX = "KEY_CHECKBOX";
  private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

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

    mCheckBox = (CheckBox) findViewById(R.id.checkBox);
    mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();

    CheckBox editTextCheck= mSharedPreferences.getString(KEY_CHECKBOX, "");
        mCheckBox.setChecked(editTextCheck);
  }

    @Override
  protected void onPause() {
        super.onPause();

        mEditor.putBoolean(KEY_CHECKBOX, mCheckBox.isChecked());
        mEditor.apply();
    }
}

1 Answer

Hi Michael. Let's have a look at your code retrieving the stored value:

CheckBox editTextCheck= mSharedPreferences.getString(KEY_CHECKBOX, "");

As with a Bundle, remember that whatever you're putting in, you're getting out. It won't change type in between. In onPause(), you saved a Boolean in SharedPreferences, using putBoolean(). So here, you need to get a Boolean, not a String.

Now, because it's a Boolean, you'll have to save it in a new Boolean variable, just like Ben created a String variable to store that retrieved String value in the video.

Finally, because it's a Boolean and not a String, you can't give an empty String as the default value (""). The instructions say : "If there isn't a saved value, the CheckBox should NOT be checked."

Hint: Remember that a Boolean can only have 2 values: true or false. So which should we put as default here so that the checkbox is not checked?

Hope that helps you complete the challenge :)

michael lee
michael lee
5,179 Points

thanks a lot, Lauren. It is truly very helpful, I appreciate the detailed explanation~

You're very welcome Michael :)