title: ViewFlipper: artifacts when removing all children but the last one
body: <p>I get a number of artifacts and incorrect behavior when trying to switch views in a <code>ViewFlipper</code>. I want to add and remove views <strong>on demand</strong>, so, instead of using XML, I must call <code>ViewGroup.addView()</code>. At certain times I want to clean up the container by removing all children but the last. Here is a demo:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          LinearLayout root = new LinearLayout(this);
          root.setOrientation(LinearLayout.VERTICAL);
  
          final ViewFlipper flipper = new ViewFlipper(this);
  
          Animation in = new TranslateAnimation(-200, 0, 0, 0);
          in.setDuration(300);
          Animation out = new TranslateAnimation(0, 200, 0, 0);
          out.setDuration(300);
          flipper.setInAnimation(in);
          flipper.setOutAnimation(out);
  
          // clean it up
          out.setAnimationListener(new AnimationListener(){
  
              @Override
              public void onAnimationEnd(Animation animation) {
                  flipper.post(new Runnable(){
                      public void run() {
                          flipper.removeViews(0, flipper.getChildCount() - 1);
                      }
                  });
              }
  
              @Override
              public void onAnimationRepeat(Animation animation) {}
  
              @Override
              public void onAnimationStart(Animation animation) {}
          });
  
          Button button = new Button(this);
          button.setText("Click me");
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  View a = makeView();
                  flipper.addView(a);
                  flipper.showNext();
              }
          });
  
          root.addView(button);
          root.addView(flipper);
          setContentView(root);
      }
  
      int i = 0;
  
      public View makeView() {
          TextView tv = new TextView(MainActivity.this);
          tv.setText("TextView#" + i++);
          tv.setTextSize(30);
          return tv;
      }
  }
  </code></pre>
  
  <p>Sometimes I want to remove all children but the last added, to save memory and because those children will never be used again (Maybe I can recycle them, but that's another story). I use a simple runnable scheduled via <code>View.post()</code> in the animation listener, and exactly once every three times there are artifacts.</p>
  
  <p>Using <code>View.post(Runnable)</code> is <strong>required</strong> because if you remove children directly in the animation listener, <code>NullPointerException</code> are thrown because (at least on Honeycomb+, which uses display lists to draw hierarchies).</p>
  
  <p><strong>Note</strong>: I'm developing for 2.1+, so the Honeycomb animation packages doesn't suit.</p>