title: Starting Frame-By-Frame Animation
body: <p>I have a basic question about starting a frame-by-frame animation.</p>
  
  <p>When I call the <strong>AnimationDrawable.start()</strong> method from my code directly, it doesn't seem to work.</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {  
     ...  
     mAnimation.start();  
     ...  
  }
  </code></pre>
  
  <p>But if I put this line inside the onClick() callback method of a button, pressing the buton starts the animation.</p>
  
  <p>Why doesn't this line work in the code? </p>
  
  <p>Thanks!</p>
  
  <h2>Code:</h2>
  
  <pre><code>public class MyAnimation extends Activity {
  @Override
  
  public void onCreate(Bundle savedInstanceState) {
  
      AnimationDrawable mframeAnimation = null;
      super.onCreate(savedInstanceState);
      setContentView(R.layout.my_animation);
  
      ImageView img = (ImageView) findViewById(R.id.imgMain);
  
      BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable(
              R.drawable.splash1);
      BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable(
              R.drawable.splash2);
  
      int reasonableDuration = 250;
      mframeAnimation = new AnimationDrawable();
      mframeAnimation.setOneShot(false);
      mframeAnimation.addFrame(frame1, reasonableDuration);
      mframeAnimation.addFrame(frame2, reasonableDuration);
  
      img.setBackgroundDrawable(mframeAnimation);
  
      mframeAnimation.setVisible(true, true);
      //If this line is inside onClick(...) method of a button, animation works!!
      mframeAnimation.start(); 
  }
  </code></pre>
  
  <p>}</p>