0

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?

Allrounder
  • 685
  • 2
  • 9
  • 20
  • I would save it upon check/uncheck and not wait for any other user action... it's the "fashionable" way of doing it nowadays (cfr iOs toggle buttons, you toggle it and that's it, you don't have to "submit" your changes) – Laurent S. Apr 03 '13 at 13:05
  • Please see http://stackoverflow.com/questions/151777/saving-activity-state-in-android?rq=1 –  Apr 03 '13 at 13:06
  • instead of using database as Arju suggest, the 'lightest' way will be to store some variables in `SharedPreference` and populate your interface depending on them. – hardartcore Apr 03 '13 at 13:06
  • Thanks It all works now, Thank you all for your help. Really appreciate it! – Allrounder Apr 03 '13 at 13:20

4 Answers4

0

Use Database to store the checkbox value retrive it later for your Use

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

I see you tried to use sharedPreferences and you are almost there, let me show you basic example which you can modify a bit:

// save boolean in sharedPreferences (this code goes in if(ch.isChecked()))
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("some_key", true); // here true is the value you want to save if the checkbox is checked
editor.commit();                    

// restore string in sharedPreferences (usually you have this onCreate method) 
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
checkbox.setChecked(settings.getBoolean("some_key", "false"));
Triode
  • 11,309
  • 2
  • 38
  • 48
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
0

You should do it in the onPause method:

  @Override
    protected void onPause() {
        super.onPause();
         if(ch.isChecked())
            {

        SharedPreferences settings = getSharedPreferences(PREFRENCES_NAME, 0);
        ch.setChecked(settings.getBoolean("cbx1_ischecked" ,true));
        settings.edit().putBoolean("check",true).commit();
                    }
    else
    {
          SharedPreferences settings = getSharedPreferences(PREFRENCES_NAME, 0);
        ch.setChecked(settings.getBoolean("cbx1_ischecked" ,false));
        settings.edit().putBoolean("check",false).commit();
    }}


}

So whenever the activity is destroyed or left the onPause method is called and you save the state of checkbox.

Gyonder
  • 3,674
  • 7
  • 32
  • 49
0

Your code only saves the state, but you have to reset it when the Activity resumes. You should override the onResume function and set the state of the checkboxes according to the saved settings.

pshegger
  • 2,526
  • 24
  • 29