I have a checkbox on my activity. I'm wondering how to make its state stay the same (checked/unchecked) everytime I open my app.
5 Answers
You can use SharedPreferences to implement CheckBox which retains its state even if application is closed.
- When user checks/un-check CheckBox.Save it's state in Shared Preferences.
- Whenever user Opens your activity. Read previously saved value from Shared-Preferences, and set the state of check box.
Here is an Example code to for Saving state of checkbox even if app is closed.
public class TestActivity extends Activity{
CheckBox checkBox = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox) findViewById(R.id.my_check_box);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/my_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

- 19,140
- 8
- 51
- 70
checkBox.setChecked(true);
If you want the last state, simply write the checked state to SharedPreferences
in the onPause()
method and get the state in the onResume()
method.
Reference: http://developer.android.com/reference/android/widget/CheckBox.html http://developer.android.com/reference/android/content/SharedPreferences.html

- 16,805
- 9
- 69
- 108
Your best option is to implement SharedPreferences
in your app and save it's state. When you run your app you retrieve the status from the preferences and check/uncheck the checkbox.

- 86,916
- 18
- 197
- 190
In order to remember application state, you will want to make use of the onSaveInstanceState()
and onRestoreInstanceState()
methods (see this answer for more info) to determine if the box should be checked or unchecked...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("IsCheckboxChecked", _myCheckbox.isChecked());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
_myCheckbox.setChecked(savedInstanceState.getBoolean("IsCheckboxChecked"));
}

- 1
- 1

- 9,644
- 6
- 44
- 56
-
is the code above enough to get what I want? How should I apply it to my checkbox? – hectichavana Jan 31 '12 at 14:14
-
1see `_myCheckbox.setChecked` it applies the state to the checkbox. – Leandros Jan 31 '12 at 14:19
-
I've applied those snippets on my app, but it doesn't save the checkbox' state – hectichavana Jan 31 '12 at 14:49
-
Are you setting your checkbox to _myCheckbox? For example: `CheckBox _myCheckbox = (Checkbox) findViewById(R.id.my_checkbox);` – Ryan Berger Jan 31 '12 at 15:09
-
It seems that calling `super.onSaveInstanceState(savedInstanceState);` after setting values can cause problems. I have updated my snippets to reflect this. – Ryan Berger Jan 31 '12 at 15:28
Your best bet is to use SharedPreference. You can use it like this:
Save the current state to sharedpreference:
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit(); //opens the editor
editor.putBoolean("isChecked", true); //true or false
editor.commit(); //saves it in shared preference
then when your activity starts you can check that value in the SharedPreference like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
checkBox.setChecked(sharedPreferences.getBoolean("isChecked", false));
}
Hope you can use this information

- 473
- 2
- 7
- 25
-
so for the first snippet I should make a method out of it or what? – hectichavana Jan 31 '12 at 14:38
-
Make a method that saved the state of your checkbox. So that every time the user checks or unchecks it the current state is saved. Then you can check at any place in the activity if the last known state was checked or unchecked. – Fergers Jan 31 '12 at 15:47