Just come to polishing my application and making it resume after the user has left. When the application restores I get an IllegalThreadStateException, which is quite annoying.
So in the code, when surfaceDestroyed()
is called, it sets mRun
to false and calls thread.join()
. This causes the thread to complete and die. When the app is started again and surfaceCreated()
is called, it calls thread.start()
. This is invalid because the thread can not be started after it dies.
Two options to fix:
a) Start a new thread in surfaceCreated()
- as above.
b) Or add a check in surfaceDestroyed()
against Activity.isFinishing()
to only end the thread if true. To do this, I surrounded the while(mRun)
in the thread with another while loop that is only set to false if isFinishing()
returns true.