title: Android game thread lifecycle implementation
body: <p>So I am working on a new basic 2d game for Android and was setting up my usual implementation of a game thread working with a SurfaceView. While typing away, I realized that, while this seems to work, I would really like to know if there is a better way to achieve the same results. Here are the basics of what I do right now (I apologize for the verbosity, I'm trying to compress as much as possible):</p>
  
  <p>GameActivity class</p>
  
  <pre><code>@Override
  public void onCreate(final Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      this.setContentView(R.layout.game);
  
      this.mGameView = (GameView)this.findViewById(R.id.gameview);
  }
  </code></pre>
  
  <p>XML layout</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;merge xmlns:android="http://schemas.android.com/apk/res/android"&gt;
   &lt;com.package.name.GameView
      android:id="@+id/gameview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:focusable="true"
          android:focusableInTouchMode="true" /&gt;
  &lt;/merge&gt;
  </code></pre>
  
  <p>GameView</p>
  
  <pre><code>@Override
  public void surfaceCreated(final SurfaceHolder holder) { }
  
  @Override
  public void surfaceChanged(final SurfaceHolder holder, final int format, 
                              final int width, final int height)
  {
      this.mGameThread = new GameThread(this.getContext(), holder, 
                                          width, height);
  
      this.mGameThread.start(); // starts the game
  }
  
  @Override
  public void surfaceDestroyed(final SurfaceHolder holder)
  {
      // flips volatile flag stopping execution
      // then calls .join()
      this.mGameThread.release(); 
  }
  </code></pre>
  
  <p>GameThread class</p>
  
  <pre><code>@Override
  public void run()
  {
      // load game state
      while (this.mRunFlag) // volatile loop flag
      {
          // game loop
      }
      // save game state
  }
  </code></pre>
  
  <p>As you can see, basically I depend on the SurfaceHolder's callbacks to manage the game. This works well as orientations change, etc. but without asking, I may never know if this can be achieved in a better way, which I would be anxious to learn if there is. So that's why I'm asking! Any thoughts?</p>
  
  <p>(The reason I'm so curious is because I almost entirely ignore the Activity life-cycle events, and feel this may be a problem...)</p>