how to pause and resume a surfaceView thread

前端 未结 8 1284
野趣味
野趣味 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:30

    Tried to comment on the accepted answer above but couldn't, new to this. I don't think you should be calling your start/stop thread methods from both your SurfaceView and Activity. This will result in starting/stopping the thread doubly, and you can't start a thread more than once. Just call your methods from the Activity's onPause and onResume. They're called when exiting and re-entering the app so this will make sure your states are handled properly. surfaceDestroyed isn't always called, which messed me up for a while.

    If you use this method make sure to check for a valid surface in your run code before working with your canvas, because the Activity will start the thread in onResume before the surface is available.

            while (_run) {
                if (_surfaceHolder.getSurface().isValid()) {
                    ...
                }
            } //end _run
    

提交回复
热议问题