title: Android canvas game loop breaks onResume() after closing and reopening app
body: <p>On a fresh install, the first time I open the app, I don't really have any problems. However, If I were to kill the app via processes, force stop, or simply press the home button and re launch the app, I run into a few problems...</p>
  
  <p><strong>First problem</strong> - when the app is relaunched after closed or killed, there is a long delay before actually launching the app window. Also, sometimes it takes up to 30 seconds for the game graphics (whats being drawn to the canvas) to display. All I will see is a black screen while I hear the game sound effects playing in the background, as if it were running.</p>
  
  <p><strong>Second problem</strong> - After waiting about 30 seconds for the app to actually display the canvas, sometimes the on touch listener gets unregistered or something. I'm unable to interact with the game as if my touches were not registering. Once again, this only happens on a RELAUNCH after the app has successfully ran once and then closed / killed.</p>
  
  <p><strong>Third problem</strong> - in the case that I get impatient and do not wait the 30 seconds for the canvas to display, but instead tap the home button or kill the "unresponsive" app, I always get an error message saying something like "app unresponsive, wait? force close?" or "Unexpected error, App has stop running." </p>
  
  <p>So these problems have led me to believe it is an issue with how I am starting / restarting / creating my game loop:</p>
  
  <pre><code>public class MainMenuActivity extends ActionBarActivity implements View.OnTouchListener{
  
  
  MenuView menu;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      menu = new MenuView(this);
      menu.setOnTouchListener(this);
      setContentView(R.layout.activity_main_menu);
  
      Button buttonplay = (Button)findViewById(R.id.buttonPlay);
      buttonplay.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              launchCanvas();
          }
      });
  }
  
  public void launchCanvas(){
       setContentView(menu)
  }
  
  
  @Override
  public boolean onTouch(View v, MotionEvent event) {
           ////do stuff
      }
      return true;
  }
  
  
  ////////////////////////////////////////inner MenuView class
  
  public class MenuView extends SurfaceView implements Runnable {
  
      Thread t = null;
      SurfaceHolder holder;
      boolean ok;
      Game game;
  
      public MenuView(Context context){
          super(context);
          game = new Game(this);
          holder = getHolder();
      }
  
      @SuppressLint("WrongCall")
      @Override
      public void run() {
          while(ok){
              if(!holder.getSurface().isValid()){
                  continue;
              }
  
              Canvas c = holder.lockCanvas();
              onDraw(c);
              holder.unlockCanvasAndPost(c);
          }
      }
  
  
      public void onDraw(Canvas canvas){
          canvas.drawARGB(255, 1, 5, 29);
          game.draw(canvas);
      }
  
      public void pause(){
          ok = false;
          while(true){
              try{
                  t.join();
              }catch (InterruptedException e){
                  e.printStackTrace();
              }
              break;
          }
          t=null;
      }
      public void resume(){
          ok = true;
          t = new Thread(this);
          t.start();
      }
  
  
  
      }
  ////////////////////////////////////////////////////end MenuView class`
  
  
  
  @Override
  protected void onPause() {
      super.onPause();
      menu.pause();
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      menu.resume();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any ideas on how to fix?</p>
  
  <p><strong>UPDATE - Potential Problem Discovered</strong>
  I believe the root of my problems came from not letting my thread sleep. I added the following try block in the draw() method that is called every iteration of the thread loop:</p>
  
  <pre><code>try {
      Thread.sleep(10);
  } catch (InterruptedException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>Although it is a bit choppy and uneven now, I have not been able to reproduce the problems described above.</p>
  
  <p>I just changed from 10ms to sleep only 1ms, and It isn't choppy anymore AND still cannot reproduce the problems. 1ms seems kind of trivial... why is this necessary for the thread to sleep an insignificant amount of time?</p>