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
Asked
Active
Viewed 3,173 times
1
2 Answers
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
-
Though it's correct. Do it in onPause(), because this is the only callback, that's guaranteed to be called. – Fildor Sep 24 '12 at 07:53
-
I work the other way but thanks for you theoretical response,+1 – Winnie Sep 25 '12 at 01:45
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