Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Diego Marrs
Diego Marrs
8,243 Points

Stuck on challenge 4/4


I'm having trouble passing this challenge, i'm doing what it tells me to do but it still tells me that the CheckBox object needs to return 'false':

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

public class MainActivity extends Activity {
  private static final String PREFS_FILE = "com.sharedpreferencesapp.preferences";
  private static final String KEY_CHECKBOX = "KEY_CHECKBOX";
  public CheckBox mCheckBox;

  private SharedPreferences mSharedPreferences;
  private SharedPreferences.Editor mEditor;

  protected void onCreate(Bundle savedInstanceState) {

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

    if (mSharedPreferences.getBoolean(KEY_CHECKBOX, true)) {
    } else if (mSharedPreferences.getBoolean(KEY_CHECKBOX, false)) {

  protected void onPause() {

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

Please help!

1 Answer

Judah Devasahayam
.a{fill-rule:evenodd;}techdegree seal-36
Judah Devasahayam
Full Stack JavaScript Techdegree Graduate 22,610 Points

Why are you using else if? just use if else and your code should look like this

 if (mSharedPreferences.getBoolean(KEY_CHECKBOX, false)) {
    } else {

or just use 

mCheckBox.setChecked(mSharedPreferences.getBoolean(KEY_CHECKBOX, false));
Ryan Lentz
Ryan Lentz
9,191 Points

The first solution works. The second recommendation won't work because the value of mCheckBox should remain true if it was already true.