title: Certain actions produce unwanted results
body: <p>So, i'll begin with some explanation.</p>
  
  <p>I'm making a game using Android studio and it's similar to Space shooter and such games(it's fine if these games are unknown to you), but the point is that I am trying to make bullets at regular interval.</p>
  
  <p>This is the code i am using to do so (and it gives satisfactory result as needed) :-</p>
  
  <p>(In short, this piece of code add an Object Bullet to an Queue with properties like it's Top and Left location that can be used while drawing the bullets on canvas at REGULAR Intervals)</p>
  
  <pre><code>public void buildBullets()
  {
   bulletBuildingThread = new Thread("Bullets Building Thread") {
          @Override
          public void run() {
              super.run();
              double shipWidthHalf = (currentShipImage.getScaledWidth(currentShipImage.getDensity()) * 0.5);
              double shipHeightHalf = (currentShipImage.getScaledHeight(currentShipImage.getDensity()) * 0.5);
              double bulletWidthHalf = (currentShip.getBulletWidth()) * 0.5;
              double bulletHeightHalf = (currentShip.getBulletHeight()) * 0.5;
  
              while (shouldBuildBullets) {
                  do {
                      previousBulletStartTime = System.nanoTime() / 1000000;
                      bullet = new Bullet(currentShip.getLoopBulletFrame(), currentShip.getBulletUpSpeed(), currentShip.getBulletDownSpeed(),
                              currentShip.getBulletRightSpeed(), currentShip.getBulletLeftSpeed(), currentShip.getMillisBeforeNextBullet());
  
                      bullet.setLocationLeft((int) (ship_left + shipWidthHalf - bulletWidthHalf));
                      bullet.setLocationTop((int) (ship_top + shipHeightHalf - bulletHeightHalf));
                      bulletQueue.Enqueue(bullet);
                      previousBulletTimeSpan = (System.nanoTime() / 1000000) - previousBulletStartTime;
                      try {
                          Thread.sleep(bullet.getMillisBeforeNextBullet() - previousBulletTimeSpan);
                      } catch (InterruptedException e) {
                          Log.e(TAG, "run: ", e);
                      }
                  }
                  while (shouldBuildBullets &amp;&amp; !GamePlay_Activity.shouldPauseTheView());
                  bulletQueue.DequeueAll();
              }
          }
      };
      bulletBuildingThread.start();
  }
  </code></pre>
  
  <p>My main class is a SurfaceView that implements Runnalbe. The respective run method is :-</p>
  
  <pre><code>@Override
  public void run()
  {
      try
      {
          while (drawingActive)
          {
  
              frameStartTime = System.nanoTime();
              Canvas canvas = holder.lockCanvas();
              if (canvas != null)
              {
                  try
                  {
                      // Your drawing here
                      // First is Background
                      canvas.drawBitmap(currentBackgroundImage, 0, 0, null);
                      numberOfBullets = bulletQueue.getSize();
                      for(int i = 0; i &lt; numberOfBullets; i++)
                      {
                          bullet = bulletQueue.get(i);
                          tempBulletLeft = bullet.getLocationLeft() + bullet.getRightSpeed() - bullet.getLeftSpeed();
                          tempBulletTop = (bullet.getLocationTop()) - bullet.getUpSpeed() + bullet.getDownSpeed();
                          if(tempBulletTop &lt;= 0 || tempBulletTop &gt;= canvas_bottom || tempBulletLeft &lt;= 0 || tempBulletLeft &gt;= canvas_right)
                          {
                              bulletQueue.Dequeue(i);
                              numberOfBullets -= 1;
                              i -= 1;
                          }
                          else
                          {
                              bulletQueue.setLocationTop(i, tempBulletTop); //changes the location properties of bullet at index i in the queue.
                              bulletQueue.setLocationLeft(i, tempBulletLeft);
                          }
                      }
                      numberOfBulletsToDraw = bulletQueue.getSize();
                      for(int i = 0; i &lt; numberOfBulletsToDraw; i++)
                      {
                          drawBullet = bulletQueue.get(i);
                          canvas.drawBitmap(drawBullet.getBulletImage(), drawBullet.getLocationLeft(), drawBullet.getLocationTop(), null);
                      }
                      canvas.drawBitmap(currentShipImage, ship_left, ship_top, null);
  
                  }
                  finally
                  {
  
                      holder.unlockCanvasAndPost(canvas);
                  }
              }
  
              // calculate the time required to draw the frame in ms
              frameTime = (System.nanoTime() - frameStartTime) / 1000000;
  
              if (frameTime &lt; MAX_FRAME_TIME) // faster than the max fps - limit the FPS
              {
                  try
                  {
                      Thread.sleep(MAX_FRAME_TIME - frameTime);
                  } catch (InterruptedException e)
                  {
                      // ignore
                  }
              }
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I have removed non-relevant part of the code and have tried to give minimal code. If something extra is needed, i will provide it as well.</p>
  
  <p>Initially, all this works fine. But when i put my app in recent and open it several time, extra bullets are built. This video is more informative as to what is happening :-</p>
  
  <p><a href="https://gofile.io/?c=FJK21y" rel="nofollow noreferrer">Video Showing the Problem</a></p>
  
  <p>I Have tried several things such as having the main thread sleep when onDestroyed or onPause is called in the activity using this View, but nothing works.</p>
  
  <p>Can some one tell me how to rectify this. I want to have constant Speed and gap between bullets Which is not happening if i open and close the app several times from recents.</p>