onSurfaceChanged called twice

柔情痞子 提交于 2019-12-05 10:08:46

It's not a bug. Personally I don't do much on onSurfaceChanged in my class which inherits from GLSurfaceView. I only set the viewport according to the new resolution and save the current resolution dimensions to a variable.

OpenGL assets such as textures don't need to be recreated at onSurfaceChanged, instead choose the right place on the ActivityCycle to create and destroy your OpenGL assets.

I am running my game on several platforms such as Android, Amazon Fire TV and OUYA. What I found that works on all these platforms is to destroy the GL assets on onPause, onStop and onRestart but I also keep a flag to see if the assets are already destroyed so I won't destroy it twice in a raw.

I create the OpenGL assets at onCreate, onRestart and onResume(also using a flag to not create it twice). If you don't want the game to lose it's state while pausing you will have to serialize the game state and write it to a file and load it later when you resume.

Hope this helps.

Again, you can't really predict how many onSurfaceCreated you will receive especially that it might behave differently on different devices. However, you can always trust the ActivityCycle to behave as described by Google.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!