Vipul Behl
Vipul Behl

Reputation: 634

How to save status of checkbox in android

I have gone through many questions on StackOverflow but could not get this code working. The checkbox is always checked when the activity is created.

My Code :

    @Override
        public void onStart() {
            super.onStart();
            chb1.setChecked(load1());
        }

        @Override
        public void onPause() {
            super.onPause();
            save1(chb1.isChecked());
        }

        @Override
        public void onResume() {
            super.onResume();
           chb1.setChecked(load1());
        }
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_screen_lock);
    chb1 = (CheckBox)  findViewById(R.id.checkBox2);
    chb1.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(chb1.isChecked())
                    {
                        SharedPreferences sharedPreferences1 = getPreferences(Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences1.edit();
                        editor.putBoolean("checked", true);
                        editor.commit();
    }
private boolean load1() { 
        SharedPreferences sharedPreferences1 = getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences1.getBoolean("checked", false);
    }
private void save1(final boolean isChecked) {

            SharedPreferences sharedPreferences1 = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences1.edit();
            editor.putBoolean("checked", true);
            editor.commit();
    }

How can i make this work? Any help is appreciated.

Upvotes: 0

Views: 789

Answers (2)

spicydog
spicydog

Reputation: 1714

You don't need onStart since it will run onResume anyway.

Another point is you set listener on checkbox and only save when the checkbox is checked, so it will never set to false, and that's why your checkbox always checked when you open the activity.

Here is a fixed version, I hope it's work. :)

@Override
public void onPause() {
    super.onPause();
    save1(chb1.isChecked());
}

@Override
public void onResume() {
    super.onResume();
    setCheckBoxFromSave();
}

private void setCheckBoxFromSave() {
    bool loadedChecked = load1();
    if (chb1.isChecked() != loadedChecked) {
        chb1.setChecked(loadedChecked);
    }
}

private static final String KEY_CHECKED_SAVE = "checked";

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

    chb1 = (CheckBox) findViewById(R.id.checkBox2);
    setCheckBoxFromSave();
    chb1.setOncheckListener(new CheckBox.OnCheckListener() {
        @Override
        public void onCheck(boolean check) {
            save1(check);
        }
    });
}

private SharedPreferences getSharedPreferences() {
    return PreferenceManager.getDefaultSharedPreferences(this);
}

private boolean load1() { 
    return getSharedPreferences().getBoolean(KEY_CHECKED_SAVE, false);
}
private void save1(final boolean isChecked) {
    SharedPreferences.Editor editor = getSharedPreferences().edit();
    editor.putBoolean(KEY_CHECKED_SAVE, true);
    editor.apply();
}

Upvotes: 1

Hiren Patel
Hiren Patel

Reputation: 52790

You can do this way:

Save state of CheckBox by SharedPreference:

onClick of CheckBox:

SharedPreferences sharedPreferences1 = getSharedPreferences("sp_title", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putBoolean("checked", chb1.isChecked());
editor.commit();

Now get state of CheckBox:

on onCreate():

SharedPreferences sharedPreferences1 = getSharedPreferences("sp_title", Activity.MODE_PRIVATE);
boolean isChecked = sharedPreferences1.getBoolean("checked", false);

Now set state of Checkbox:

chb1.setChecked(isChecked);

Hope it will help you.

Upvotes: 2

Related Questions