title: Re-Adding views to a fragment on onResume returns an IllegalStateException
body: <p>I'm developing an Android 3.1 Tablet application with fragments.</p>
  
  <p>I've seen that only two fragments are on memory at the same time. When I show a third one, first one calls <code>onDestroyView</code>.</p>
  
  <p>I add <code>EditText</code> to fragment's view programmatically. Those <code>EditText</code> don't show again after fragment's view recreation on <code>onResume</code> method.</p>
  
  <p>I use those <code>EditText</code> to let users add data to a form and I store a reference in <code>firstTable</code> <code>HashMap</code>. I will use that <code>HashMap</code> to retrieve user's values.</p>
  
  <p>Here I create those <code>EditText</code> programmatically:</p>
  
  <pre><code>private LinearLayout createNewFirstTableRow(long articleId)
  {
      LinearLayout layout = new LinearLayout(mActivity);
      LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
      parentParams.weight = 1;
      layout.setLayoutParams(parentParams);
  
      if (firstTable == null)
          firstTable = new HashMap&lt;Long, ArrayList&lt;EditText&gt;&gt;();
  
      ArrayList&lt;EditText&gt; fields = new ArrayList&lt;EditText&gt;(7);
  
      LayoutParams params = new LayoutParams(0, 40);
      params.weight = 0.125f;
  
      TextView textView = new TextView(mActivity);
      textView.setLayoutParams(params);
      textView.setText(new Long(articleId).toString());
      layout.addView(textView);
  
      for (int i = 0; i &lt; 7; i++)
      {
          EditText edit = new EditText(mActivity);
          edit.setLayoutParams(params);
          edit.setInputType(InputType.TYPE_CLASS_NUMBER);
          fields.add(i, edit);
          layout.addView(edit);
      }
  
      firstTable.put(new Long(articleId), fields);
  
      return layout;
  }
  </code></pre>
  
  <p><code>firstTable</code> variable is a global variable: <code>private HashMap&lt;Long, ArrayList&lt;EditText&gt;&gt; firstTable;</code>.</p>
  
  <p>To add my <code>EditText</code> I do the following on <code>onResume</code>:</p>
  
  <pre><code>@Override
  public void onResume()
  {
      Log.v("QuantityFragment", "onResume: " + firstTableRowIndex);
  
      if ((firstTable != null) &amp;&amp; (secondTable != null))
      {
          firstTableRowIndex = FIRST_TABLE_ROW_INDEX;
          secondTableRowIndex = SECOND_TABLE_ROW_INDEX;
  
          LinearLayout table = (LinearLayout)view.findViewById(R.id.quantityTable);
  
          for (int index = 0; index &lt; firstTable.size(); index++)
          {
              Long articleId = articleIds.get(index);
  
              table.addView(resumeTable(articleId, secondTable.get(articleId)), secondTableRowIndex);
              table.addView(resumeTable(articleId, firstTable.get(articleId)), firstTableRowIndex);
  
              firstTableRowIndex++;
              secondTableRowIndex++;
          }
      }
  
      super.onResume();
  }
  
  private LinearLayout resumeTable(Long articleId, ArrayList&lt;EditText&gt; fields)
  {
      LinearLayout layout = new LinearLayout(mActivity);
      LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
      parentParams.weight = 1;
      layout.setLayoutParams(parentParams);
  
  
      LayoutParams params = new LayoutParams(0, 40);
      params.weight = 0.125f;
  
      TextView textView = new TextView(mActivity);
      textView.setLayoutParams(params);
      textView.setText(new Long(articleId).toString());
      layout.addView(textView);
  
      for (int index = 0; index &lt; fields.size(); index++)
      {
          layout.addView(fields.get(index));
      }
  
      return layout;
  }
  </code></pre>
  
  <p>But, here <code>layout.addView(textView);</code> I get an exception:</p>
  
  <p><strong>IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.</strong></p>
  
  <p>Is there another way to re-add those <code>EditText</code>?</p>
  
  <p><strong>UPDATE:</strong><BR/><BR/></p>
  
  <p>I have solved my problem changing <code>resumeTable</code>:</p>
  
  <pre><code>private LinearLayout resumeTable(Long articleId, ArrayList&lt;EditText&gt; fields)
  {
      LinearLayout layout = new LinearLayout(mActivity);
      LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
      parentParams.weight = 1;
      layout.setLayoutParams(parentParams);
  
  
      LayoutParams params = new LayoutParams(0, 40);
      params.weight = 0.125f;
  
      TextView textView = new TextView(mActivity);
      textView.setLayoutParams(params);
      textView.setText(new Long(articleId).toString());
      layout.addView(textView);
  
      for (int index = 0; index &lt; fields.size(); index++)
      {
          LinearLayout parent = (LinearLayout)fields.get(index).getParent();
          parent.removeView(fields.get(index));
          layout.addView(fields.get(index));
      }
  
      return layout;
  }
  </code></pre>
  
  <p>This is the important part:</p>
  
  <pre><code>    for (int index = 0; index &lt; fields.size(); index++)
      {
          LinearLayout parent = (LinearLayout)fields.get(index).getParent();
          parent.removeView(fields.get(index));
          layout.addView(fields.get(index));
      }
  </code></pre>
  
  <p><strong>The question is open, if you have a better solution, please, let me know.</strong></p>