1

Recently when writing a game I experienced a problem. In my game I used SurfaceView and I used the while(true) as the condition. After I press the home button, the program quits and I see the log. But I find the loop While(true) is still running. Why is the loop still running after the Activity has stopped? Could anybody help and tell me the reason. Thanx

CocoNess
  • 4,213
  • 4
  • 26
  • 43
Winnie
  • 769
  • 5
  • 23

2 Answers2

1

A better solution might be to set a bool to hold the state of the application and then set that to false when the game quits.

while(appActive)
    //do game logic

EDIT: Thanks to Braj and Fildor for feedback

in the onPause() event handler add a line

appActive = false;

((NOTE: I've not done any Android development, this is purely a theoretical response))

kdmurray
  • 2,988
  • 3
  • 32
  • 47
0

I do it like this in the game I'm working on now:

    //Makes the thread draw until the state of running is set to false
    while(this.running)
    {
            //Draw
    }
    this.thread.interrupt();

In onPause:

//Stop the thread that is responible for the drawing.
this.worker.running = false;

In onResume:

//Creates a new thread instance which sets this.running to true and calls the drawing method again
this.worker.restart();
Araw
  • 2,410
  • 3
  • 29
  • 57