title: Android Development: checkboxes won't save w/ SharedPreferences after leaving activity and returning
body: <p>I have some good background with Java, but I'm very new to Android and have been stuck on this problem for hours.  I've gone through every other StackOverflow article I can find dealing with similar issues, but nothing seems to have worked for me.  I'm sure I've programmed this awkwardly and my mistake is just as stupid, so please tear apart my code :)</p>
  
  <p>Anyway, I'm having issues saving checkboxes after pressing "Back" on my activity. The activity has a list of checkboxes for various email addresses, set up as you can see below via ArrayAdapter and simple_list_item_multiple_choice layout.  I can click on the checkboxes no problem; they add or remove the checkmark easily enough. I can also save it to preferences without any issue (at least I think it does).  </p>
  
  <p>However, when I press "Back" (which as I understand it destroys the activity) and then re-click on the button which calls startActivity(intent) for the list activity (which as I understand it calls onCreate again), the checkboxes that I previously selected simply refuse to save and are all unchecked.  I've tried more variations than I can count, attempted to use bundleInstance with onDestroy/onCreate instead, etc. but to no avail. My theory is that the problem may be that my code in the for loop is not setting the checkboxes I want it to set. Is this the case, and if so how do I fix it?</p>
  
  <p>(As a side note, I know that the for loop looks awkward, but I did it that way because I want it to respond to dynamic lists. Sometimes a list will have localNewsEmail and internationalNewsEmail, sometimes it'll have neither, sometimes it'll have extra ones.  So I need the checkboxes to respond to a dynamic list when i don't know precisely which checkboxes will be present and which won't.  For testing purposes, however, I've hardcoded which items to add to allEmails, and logged an error if it detects anything else.)</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_display_secondary_emails);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
          }
      });
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
      Intent intent = getIntent();
      localNewsEmail = intent.getStringExtra("emailLocal");
      internationalNewsEmail = intent.getStringExtra("emailInt");
  listViewEmails = (ListView) findViewById(R.id.listViewEmails);
              allEmails.add(localNewsEmail);
              allEmails.add(internationalNewsEmail);
  
              SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
              final SharedPreferences.Editor prefsEditor = myPrefs.edit();
  
              adapter2 = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, allEmails);
              listViewEmails.setAdapter(adapter2);
              for (int i = 0; i &lt; listViewEmails.getChildCount(); i++) {
                  if ((listViewEmails.getItemAtPosition(i)).equals(localNewsEmail)) {
                      ((CheckedTextView) listViewEmails.getChildAt(i)).setChecked(myPrefs.getBoolean(localNewsEmail, false));
                  } else if ((listViewEmails.getItemAtPosition(i)).equals(internationalNewsEmail)) {
                      ((CheckedTextView) listViewEmails.getChildAt(i)).setChecked(myPrefs.getBoolean(internationalNewsEmail, false));
                  } else {
                      Log.e("SecondaryActivity", "ERROR");
                      ((CheckedTextView) listViewEmails.getChildAt(i)).setChecked(false);
                  }
              }
  
              listViewEmails.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                      // change the checkbox state
                      CheckedTextView checkedTextView = ((CheckedTextView) view);
                      checkedTextView.setChecked(!checkedTextView.isChecked());
                      if (checkedTextView.getText().equals(localNewsEmail)) {
                          isLocalChecked = checkedTextView.isChecked();
                          prefsEditor.putBoolean(localNewsEmail, isLocalChecked);
                          prefsEditor.commit();
                      } else if (checkedTextView.getText().equals(internationalNewsEmail)) {
                          isInternationalChecked = checkedTextView.isChecked();
                          prefsEditor.putBoolean(internationalNewsEmail, isInternationalChecked);
                          prefsEditor.commit();
                      }
                  }
              });
  </code></pre>
  
  <p>Thanks for any assistance you can provide!!</p>