title: End SurfaceView and GameThread on exiting app
body: <p>I've got a surfaceview and a gameThread class.
  The gameThread keeps updating and painting on the SurfaceView class.</p>
  
  <p>Now when I exit the app (By pressing the home or back button) I get a message that the app force closed. That's because the GameThread still tries to draw on the erased surfaceview...</p>
  
  <p>So how do i properly end the app without getting this force close notification? I would like the GameThread class to stop when the back button is pressed. It should pause when pressing on home and running in the background. When re-entering the still running game it should resume....</p>
  
  <p>Any ideas? </p>
  
  <p>This is my GameThread class:</p>
  
  <pre><code>public class GameThread extends Thread{
  
  private GameView view;
  public boolean isRunning = false;
  
  public GameThread(GameView view) {
      this.view = view;
  }
  
  public void setRunning(boolean setRunning) {
      isRunning = setRunning;
  }
  
  public void run() {
      while(isRunning) {
          Canvas c = null;
          view.update();
          try {
              c = view.getHolder().lockCanvas();
              synchronized (view.getHolder()) {
                  view.draw(c);
              }
          }finally {
              if(c != null) {
                  view.getHolder().unlockCanvasAndPost(c);
              }
          }
      }
  }
  </code></pre>
  
  <p>It keeps updating my GameView class: </p>
  
  <p>public class GameView extends SurfaceView{</p>
  
  <pre><code>private GameThread gameThread;
  
  public GameView(Context context, Activity activity) {
      super(context);
      gameThread = new GameThread(this);
      init();
      holder = getHolder();
      holder.addCallback(new SurfaceHolder.Callback() {
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
  
          }
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              gameThread.setRunning(true);
              gameThread.start();
  
          }
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width,
                  int height) {
          }
      });
  }
  
  
  public void init() {
  
  }
  
  public void update() {
  
  }
  
  public void draw(Canvas canvas) {
      super.draw(canvas);
  
  }
  }
  </code></pre>
  
  <p>When pressing home my logcat shows this up:</p>
  
  <pre><code>02-24 18:24:59.336: E/SurfaceHolder(839): Exception locking surface
  02-24 18:24:59.336: E/SurfaceHolder(839): java.lang.IllegalStateException: Surface has already been released.
  02-24 18:24:59.336: E/SurfaceHolder(839):   at android.view.Surface.checkNotReleasedLocked(Surface.java:437)
  02-24 18:24:59.336: E/SurfaceHolder(839):   at android.view.Surface.lockCanvas(Surface.java:245)
  02-24 18:24:59.336: E/SurfaceHolder(839):   at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:872)
  </code></pre>
  
  <p>Main activity: </p>
  
  <pre><code>public class MainActivity extends Activity {
  
  private RelativeLayout relativeLayout;
  private GameView gameView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      gameView = new GameView(this, this);
      setContentView(R.layout.activity_main);
      relativeLayout = (RelativeLayout)findViewById(R.id.mainView);
      relativeLayout.addView(gameView);
  }
  
  @Override
  public void onBackPressed() {
      // TODO Auto-generated method stub
      super.onBackPressed();
      gameView.onBackPressed();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  }
  </code></pre>