title: Why does my ArrayList add elements itself on my fragment event?
body: <p>How i have said in my question i have a fragment with a list of check boxes and an ArrayList of items choosen (they are the labels of the check boxes). When the event of a check box is fired the fragment calls the method of the interface in the main activity but in it the list of items choosen has already the item in it, although there isn't in the entire code any method that can add items to it.</p>
  
  <p>Here the code:</p>
  
  <p>MainActivity :</p>
  
  <pre><code>package com.fp.testfragment1;
  
  import java.util.ArrayList;
  
  import android.app.Activity;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.FrameLayout;
  
  import com.fp.testfragment1.MultiChoiceFragment.OnMultiSelectedListener;
  
  public class MainActivity extends Activity implements OnMultiSelectedListener
  {
      FrameLayout frameLayout1;
      FrameLayout frameLayout2;
      Button transitionButton;
      FragmentManager fragmentManager;
      MultiChoiceFragment multiChoiceFragment;
      Boolean replace;
      ArrayList&lt;String&gt; list;
      ArrayList&lt;String&gt; mylis;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          list = new ArrayList&lt;String&gt;();     
          mylis = new ArrayList&lt;String&gt;();    
  
  
          list.add("item1");
          list.add("item2");
          list.add("item3");
          list.add("item4");
          list.add("item5");
  
          transitionButton = (Button)findViewById(R.id.transitionButton);
          transitionButton.setOnClickListener(transitionListener);
  
          frameLayout1 = (FrameLayout)findViewById(R.id.frameLayout1);
          frameLayout1 = (FrameLayout)findViewById(R.id.frameLayout2);
  
          fragmentManager = getFragmentManager();
      }
  
  
  
      @Override
      protected void onStart() 
      {
          super.onStart();
  
          replace = false;
      }
  
  
  
      OnClickListener transitionListener = new OnClickListener()
      {
          @Override
          public void onClick(View v)
          {
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  
              multiChoiceFragment = new MultiChoiceFragment(MainActivity.this, list, mylis, 8);
  
              fragmentTransaction.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out);
  
              if(replace)
              {
                  fragmentTransaction.replace(R.id.frameLayout1, multiChoiceFragment);    
              }
              else
              {
                  fragmentTransaction.add(R.id.frameLayout1, multiChoiceFragment);
                  replace = true;
              }
  
  
              fragmentTransaction.addToBackStack(null);
  
              fragmentTransaction.commit();
          }
  
      };
  
  
  
      @Override
      public void onGroupMCheckListener(String itemName, Boolean checked, int nf) 
      {
          if(nf == 8)
          {
              //here i have the list already full
  
  
              // this is for debug
              mylis = mylis;
  
          }
      }
  
  }
  </code></pre>
  
  <p>MultiChoiceFragment :</p>
  
  <pre><code>package com.fp.testfragment1;
  
  import java.util.ArrayList;
  import android.app.Activity;
  import android.app.Fragment;
  import android.content.Context;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.CompoundButton.OnCheckedChangeListener;
  import android.widget.LinearLayout;
  import android.widget.TableRow;
  import android.widget.TextView;
  
  public class MultiChoiceFragment extends Fragment
  {
      Context context;
      OnMultiSelectedListener mCallback;
      LinearLayout groupsListLinearLayout;
      TableRow congiungiTableRow;
      TextView groupsTextView;
      ArrayList&lt;String&gt; groups;
      ArrayList&lt;String&gt; groupsChoosen;
      int nFragment;
  
      public MultiChoiceFragment(Context context, ArrayList&lt;String&gt; groups, ArrayList&lt;String&gt; groupsChoosen, int nFragment)
      {
          this.context = context;
          this.groups = new ArrayList&lt;String&gt;();
          this.groupsChoosen = new ArrayList&lt;String&gt;();
          this.groups = groups;
          this.groupsChoosen = groupsChoosen;
          this.nFragment = nFragment;
      }
  
  
      public MultiChoiceFragment()
      {
          groups = new ArrayList&lt;String&gt;();
          groupsChoosen = new ArrayList&lt;String&gt;();
      }
  
  
      public interface OnMultiSelectedListener
      {
          public void onGroupMCheckListener(String itemName, Boolean checked, int nf);
      }
  
  
      @Override
      public void onAttach(Activity activity)
      {
          super.onAttach(activity);
  
          try 
          {
              mCallback = (OnMultiSelectedListener) activity;
          } 
          catch (ClassCastException e)
          {
              throw new ClassCastException(activity.toString() + " must implement OnMultiSelectedListener");
          }
  
          context = activity;     
  
  
      }   
  
      @Override
      public void onSaveInstanceState(Bundle outState)
      {
          outState.putStringArrayList("groups", groups);
          outState.putStringArrayList("groupsChoosen", groupsChoosen);
          outState.putInt("nFragment", nFragment);
  
          super.onSaveInstanceState(outState);
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      {
          View v = inflater.inflate(R.layout.choice_fragment, container, false);
  
          groupsListLinearLayout = (LinearLayout)v.findViewById(R.id.groupsLinearLayout);
          groupsTextView = (TextView)v.findViewById(R.id.groupsTextView);
          congiungiTableRow = (TableRow)v.findViewById(R.id.congiungiTableRow);
  
          if(savedInstanceState != null)
          {
              groups = savedInstanceState.getStringArrayList("groups");
              groupsChoosen = savedInstanceState.getStringArrayList("groupsChoosen");
              nFragment = savedInstanceState.getInt("nFragment");
          }
  
          refreshListGUI();
  
          return v;
      }
  
  
      public void refreshListGUI() 
      {       
  
          for(int a = 0; a &lt; groups.size(); a++)
          {
              CheckBox checkBox = new CheckBox(context);
              checkBox.setId(a);
              if(groupsChoosen == null || groupsChoosen.isEmpty() == true)
              {
                  checkBox.setChecked(false);
              }
              else if(groupsChoosen.isEmpty() == false &amp;&amp; groupsChoosen.contains(groups.get(a)))
              {
                  checkBox.setChecked(true);
              }
  
              checkBox.setText((String)groups.get(a));
              checkBox.setOnCheckedChangeListener(checkBoxMItemListener);
  
              groupsListLinearLayout.addView(checkBox);
          }       
  
  
      }
  
  
      OnCheckedChangeListener checkBoxMItemListener = new OnCheckedChangeListener()
      {
  
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              String groupC = buttonView.getText().toString();
  
              if(isChecked == true &amp;&amp; groupsChoosen.contains(groupC) == false)
              {
                  groupsChoosen.add(groupC);
                  mCallback.onGroupMCheckListener(groupC, isChecked, nFragment);
              }
              else if(isChecked == false &amp;&amp; groupsChoosen.contains(groupC) == true)
              {
                  groupsChoosen.remove(groupC);
                  mCallback.onGroupMCheckListener(groupC, isChecked, nFragment);
              }
  
          }
  
      };
  
  
  
  
  
  }
  </code></pre>
  
  <p>PS. I use the nf in another project</p>