javaonresumeonpausecheckedtextview

Problem with saving checkedtextview state, it misbehaves with multiple items


I made an activity consisting of CheckTextView's and TextView's. When the user checks the box, I want to save that state when the user leaves the activity or closes the app.

I added onClickListener to every CTV.

Then I try to save it in onPause and onResume methods. I can't troubleshoot this problem as the checkboxes work when I save just a few of them it works (it varies but it works with 1-5 of them) but when I add all of them they are not saved when I go back to the activity.

//this will always work and will save the state of the boxes


protected void onPause() {
        super.onPause();

        save(ctv1.isChecked());
        save(ctv2.isChecked());
        save(ctv3.isChecked());


}

protected void onResume() {
        super.onResume();


        ctv1.setChecked(load());
        ctv2.setChecked(load());
        ctv3.setChecked(load());
}


//when I add all of them, they are always either checked or unchecked
//it doesn't matter what combination of them I try, it seems that it is //always working with a couple of CTV's but fails with more than 5-6 of them



//this is how my onClickListener looks like

     CheckedTextView ctv1 = (CheckedTextView) findViewById(R.id.ctvFOX1);
      ctv1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (ctv1.isChecked()) {
                  ctv1.setChecked(false);


              }
              else {
                  ctv1.setChecked(true);

              }
          }
      });




//save and load methods


private void save(final boolean isChecked) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.apply();
}

private boolean load() {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", false);
    }


Solution

  • Because you only use one key to save the CheckedTextView's value!

     private void save(final boolean isChecked, String key) {
            SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(key, isChecked);
            editor.apply();
        }
    
        private boolean load(String key) {
            SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
            return sharedPreferences.getBoolean(key, false);
        }
    
        protected void onPause() {
            super.onPause();
            save(ctv1.isChecked(), "check1");
            save(ctv2.isChecked(), "check2");
            save(ctv3.isChecked(), "check3");
    
    
    
        }
    
        protected void onResume() {
            super.onResume();
            ctv1.setChecked(load("check1"));
            ctv2.setChecked(load("check2"));
            ctv3.setChecked(load("check3"));
    
        }