First of all I would like to thank you because your blog and the information you provide have been a tremendous help for me so far.
I am a new android developer in the middle of creating a simple game with a SurfaceView and a thread (like the Lunar Lander example).
I noticed then when the surface is destroyed or something happens such as an incoming call or the keyboard orientation is changed, it leaves the application in an unpredictable state and it often times crashes.
I was wondering how you would handle the pausing/unpausing of the thread and game in accordance with the Activity lifecycle.
My main loop looks something like this:
if (mGameState == STATE_RUNNING)
Right now I create the game thread in the onStart() method and "pause" it immediatley and "resume" it once the Surface is created. The thread stops in the onStop() method of the activity and threadRunning is set to false.
The problem I'm having is when the thread is still running but the game is not in the foreground. I want to keep the game's state but I don't want to busy-wait in the main loop.
The research I did so far suggested that wait() and notify() should be used with synchronization to pause the thread, but how exactly would I go about doing that? Or is there a better way to do it? If so, please enlighten us with your infinite knowledge. :)