how to pause and resume a surfaceView thread

前端 未结 8 1185
野趣味
野趣味 2020-12-02 08:21

I have a surfaceView setup and running, but when I resume it I get an error that the thread has already been started. What\'s the proper way to handle when the app goes to t

相关标签:
8条回答
  • 2020-12-02 08:47

    The easy solution is to simply kill and restart the thread. Create methods resume() - creates thread object and starts it - and pause() - kills thread (see Lunarlander example) - in your SurfaceView class and call these from surfaceCreated and surfaceDestroyed to start and stop the thread.

    Now in the Activity that runs the SurfaceView, you will also need to call the resume() and pause() methods in the SurfaceView from the Activity's (or fragment's) onResume() and onPause(). It's not an elegant solution, but it will work.

    0 讨论(0)
  • 2020-12-02 08:57

    You should use the Activities onPause() and onResume() methods.

    First, in surfaceCreated(), start the thread. Also, in onResume(), make sure the thread isn't already started (keep a variable inside the thread or something). Then if it is not running, set it as running again. in onPause(), pause the thread. In surfaceDestroyed, pause the thread again.

    0 讨论(0)
提交回复
热议问题