Android: Set check box state into shared preference
<p>I am saving my check box selected state in shared preference but its not working pls check my code below..</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting);
    CheckBox cb = (CheckBox) findViewById(R.id.check1);
    CheckBox cb2 = (CheckBox) findViewById(R.id.check2);
    CheckBox cb3 = (CheckBox) findViewById(R.id.check3);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            if (isChecked) {   
                CheckBox cb1 = (CheckBox) findViewById(R.id.check1);
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                cb1.setChecked(settings.getBoolean("MY_NAME",true));
            }
        }

    });

    cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
              CheckBox cb1 = (CheckBox) findViewById(R.id.check1);
              CheckBox cb2 = (CheckBox) findViewById(R.id.check2);
              cb1.setChecked(true);
              cb2.setChecked(true);
            }
        }
    });
}

public void onBackPressed() {     
    CheckBox cb = (CheckBox) findViewById(R.id.check1);
    CheckBox cb2 = (CheckBox) findViewById(R.id.check2);
    CheckBox cb3 = (CheckBox) findViewById(R.id.check3);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    // cb.setChecked((settings.getBoolean("PREFS_NAME",false))); 
    Editor editor = settings.edit();
    editor.putBoolean("MY_NAME", cb.isChecked());
    editor.putBoolean("MY_NAME", cb2.isChecked());
    editor.putBoolean("MY_NAME", cb3.isChecked());
    editor.commit();
    setResult(RESULT_OK);
    Intent i = new Intent(setting.this, MainMenu.class);
    startActivity(i);
} 
</code></pre>
