0

I'm using OpenGL ES 2.0 on Android to make a basic game. I discovered that if I hit the home key on my device (emulator or real device) when the GLSurfaceView is present and then log back into the app from the Android home screen the app will crash. In contrast, if I I hit the back key while the GLSurfaceView is present which then takes me back to my MainActivity / MainView then everything is fine. I assume this has to do with how the GL Thread is managed, and when I close the app immediately the state is saved as opposed to being popped off the activity stack like when I hit the back button to go to my MainActivity.

My question is how should I best deal with destroying the GLSurfaceView state information? If the user hits the home key I want the information to reset and not be saved. Should I override onStop in the class that implements GLSurfaceView.Renderer and delete the GLSL program?

I can give a rough picture of how my activites are laid out below.

MainActivity class:

public MainActivity extends Activity GameView view; public onCreate ( ... ) { }

GameView class:

public GameView extends GLSurfaceView Renderer renderer; public onCreate (...) { // set EGL information and renderer };

Renderer class:

public Renderer implements GLSurfaceView.Renderer // implements the surface change, created, and draw methods

BlazePascal
  • 391
  • 5
  • 12

2 Answers2

0

Make sure that your android version supports OpenGL ES 2.0 rendering on it's background state. Because whenever you press the home key app enters background state and gives background thread for your application, that may cause crashes. Mostly in iOS and android it is best to identify the app state and pause the rendering in background state. Refer further in this link Run Android OpenGL in Background as Rendering Resource for App?

Community
  • 1
  • 1
VivekParamasivam
  • 1,086
  • 2
  • 13
  • 23
0

I found out that I needed to override the 'onSurfaceDestroyed( SurfaceHandle handle) method in the class that extends GLSurfaceView and clean up my resources.

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
    super.surfaceDestroyed(holder);
    ResourceManager.getInstance().cleanUp();
}
BlazePascal
  • 391
  • 5
  • 12
  • I am facing same problem but i cannot find `ResourceManager` class? Is it user-defined class? If not, please tell the fully classified class name. – Malwinder Singh Mar 17 '15 at 08:59
  • @M.S. I should've specfiied that `ResourceManager` is a user-defined class. It simply holds my Shader and Texture objects (its a singleton class). – BlazePascal Mar 18 '15 at 04:32