I have in my app a few checkboxes (they don't do anything just put a tick in it) How can I save this so when user leaves the app it will save the checked checkbox?
At the moment when I press the home button it keeps the checkbox checked when I go back into the app. But when I exit the app (by using the back key) it doesn't save the checked checkbox.
Here is my code:
private CheckBox ch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch = (CheckBox) findViewById(R.id.checkBox1);
ch.setOnClickListener(new View.OnClickListener() {
private String PREFRENCES_NAME;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(ch.isChecked())
{
SharedPreferences settings = getSharedPreferences(PREFRENCES_NAME, 0);
ch.setChecked(settings.getBoolean("cbx1_ischecked" ,true));
settings.edit().putBoolean("check",false).commit();
Toast.makeText(getApplicationContext(), "Check", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Uncheck", Toast.LENGTH_SHORT).show();
}}
});
}
Can Anyone please help me?