title: LinearLayout.removeAllViews doesn't work with programmatically created elements
body: <p>I'm trying to programmatically create UI elements in Android. It is working, but when I press back button and after that return to the activity that creates elements, I want to delete previous created ones. I tried the following code in my onCreate method:</p>
  
  <pre><code>((LinearLayout) findViewById(R.id.mainContainer)).removeAllViews();
          ((LinearLayout) findViewById(R.id.mainContainer)).addView(generateElementsDynamically());
  </code></pre>
  
  <p>My generateElementsDynamically() method:</p>
  
  <pre><code>public static LinearLayout generateElementsDynamically()
      {
          LinearLayout main = new LinearLayout(context);
          main.setOrientation(LinearLayout.VERTICAL);
      LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      main.setLayoutParams(layoutParams1);
  
      for (int i = 0; i &lt; ServicesStorage.listServiceDetails.size(); i++)
      {
          LinearLayout lin = new LinearLayout(context);
          for (int j = 0; j &lt; ServicesStorage.listServiceDetails.get(i).size(); j++)
          {
              lin.setOrientation(LinearLayout.HORIZONTAL);
              LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
              lin.setLayoutParams(layoutParams);
              TextView txt = new TextView(context);
              txt.setText(ServicesStorage.listServiceDetails.get(i).get(j).toString());
              txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
              lin.addView(txt);
          }
          main.addView(lin);
      }
      return main;
  }
  </code></pre>
  
  <p>Any idea what is wrong?</p>
  
  <p>Thanks</p>