title: Unfortunate, myAppName has stopped. Thread already started - Kotlin
body: <p>I really need help here. I'm sorry to ask this that may look simple. It's about threads. I just start learning about it. I have looked all over the internet, but nothing solve my problem.</p>
  <p>My app work when I start running it. But, if it loose focus and resume, it crash. It gives a &quot;Unfortunately, myAppName has stopped.&quot; and I got &quot;java.lang.IllegalThreadStateException: Thread already started&quot;.</p>
  <p>This is my MainActivity:</p>
  <pre><code>    class MainActivity : AppCompatActivity() {
      private var snakeGame: SnakeGame? = null
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          val size = Point()
          supportActionBar?.hide()
          requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
          windowManager.defaultDisplay.getSize(size)
          snakeGame = SnakeGame(this, size)
          snakeGame?.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
          setContentView(snakeGame)
      }
  
  
      override fun onResume() {
          super.onResume()
          snakeGame?.resume()
      }
  
      override fun onPause() {
          super.onPause()
          snakeGame?.pause()
      }
  }
  
  </code></pre>
  <p>This is my GameView:</p>
  <pre><code>
  @SuppressLint(&quot;ViewConstructor&quot;)
  class SnakeGame(context: Context, size: Point)
      : SurfaceView(context), Runnable {
      private lateinit var touchDown: Point
      private lateinit var arena: Arena
      private lateinit var snake: Snake
      private lateinit var food: Food
  
      private val bgColor = Color.argb(255, 0, 0, 0)
      private val buttons = ArrayList&lt;ControlButton&gt;()
      private var gameThread = Thread(this)
      private val w = size.x
      private val h = size.y
  
      private var playing = false
      private var paused = true
      private var border = Rect()
      private var score = 0
      private var gameSpeed = 150
  
  
      override fun run() {
          var sameFrame = false
          var sfTime = 0L
          var acmTime: Long
          while (playing) {
              if (!paused) {
                  if (sameFrame) {
                      acmTime = System.currentTimeMillis() - sfTime
                      if (acmTime &gt; gameSpeed) {
                          update()
                          sameFrame = false
                      }
                  } else {
                      sfTime = System.currentTimeMillis()
                      sameFrame = true
                  }
              }
              draw()
          }
      }
  
      private fun setup() {
          arena = Arena(w - arenaCenter, h / 2, arenaW, arenaH)
          border = arena.sq
          snake = Snake(border.left, border.top)
          food = Food(border.left, border.top)
          if (buttons.size &lt; 1) {
              createButtons(h)
          }
      }
  
      private fun update() {
          snake.update(border.left, border.top, border.right, border.bottom)
          if (snake.die()) {
              paused = true
          }
          if (snake.eat(food.fPos)) {
              food = Food(border.left, border.top)
              score++
          }
      }
  
      private fun draw() {
          if (holder.surface.isValid) {
              c = holder.lockCanvas()
              c.drawColor(bgColor)
              val paint = Paint().apply {
                  color = Color.WHITE
                  textSize = 70f
              }
              c.drawText(&quot;Score: $score&quot;, 20f, 50f, paint)
  
              arena.show()
              food.show()
              snake.show()
              for (cb in buttons) {
                  cb.show()
              }
              holder.unlockCanvasAndPost(c)
          }
      }
  
      fun pause() {
          playing = false
          try {
              gameThread.join()
          } catch (e: InterruptedException) {
              Log.e(&quot;Error&quot;, &quot;joining thread&quot;)
          }
      }
  
      fun resume() {
          playing = true
          setup()
          gameThread.start()
      }
  }
  
  </code></pre>