title: State Machine and Intents
body: <p>My android app needs to run through a sequence of events as in a state machine</p>
  
  <p>Event 1: play video # 1</p>
  
  <p>Event 2: load an image and wait for a button press</p>
  
  <p>Event 3: play video #2</p>
  
  <p>etc...</p>
  
  <p>One way of doing would be to generate a separate activity for each event, but with over 20 events, I thought there's an easier way of doing it.</p>
  
  <p>So I coded a state machine like this:</p>
  
  <pre><code>    int mState = 0;     
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      //
      switch(mState) {
      case 0:
          Boolean flag = videoIntent(url0); 
          break;
      case 1:
          flag = loadImage(img1);
      }
      }
  
  
  public Boolean videoIntent(String video) {
       mBundle.putString("url",video ); 
       Intent myIntent = new Intent();
       myIntent.putExtras(mBundle);
           myIntent.setClass(mySM.this, SM_vPlayer.class);
           startActivity(myIntent);
               mState ++;
      return true;     
   }
  
  public Boolean loadImage(String image) {
      //load image
              mState ++;
      return true;
  }
  </code></pre>
  
  <p>Questions: After starting the intent to play the video (that activity has a listener to wait for the completion) and then finish() is called.</p>
  
  <ol>
  <li><p>Where will finish() come back to onCreate, onResume or another method?</p></li>
  <li><p>How do I get back to the switch statement?</p></li>
  <li><p>Any better ways of doing this?</p></li>
  </ol>