views:

280

answers:

2

I'm writing an Android application that uses OpenGL ES (GLSurfaceView and GLSurfaceView.Renderer). The problem is that when the user switches applications and then switches back to my app, the GLSurfaceView destroys and recreates the GL context. This is what it's supposed to do according to the documentation but is there a way to prevent this from happening?

It takes a long time to load textures into the context and i'd like to prevent having to reload them.

A: 

It's been a while since I worked with OpenGL and it was the standard sort on desktop pc's, but I seem to remember standard OpenGL doesn't require a reload of textures on context switch. Of course, that doesn't really help in this case.

Assuming the textures have to be reloaded, the question becomes: how do you speed this up? And then the question becomes, just how many textures do you need at any one time and can you load them on demand? What are their dimensions? I recall that powers of two were usually faster to load, though that may also depend on the OpenGL implementation and drivers.

I've also heard about keeping a context somewhere where it won't get destroyed, somewhat like this thread: http://stackoverflow.com/questions/1228067/opengles-view-switching-problem

GenericMeatUnit
+2  A: 

I think what you are looking for is discussed in the GLSurfaceView documentation:

A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

In your activity, ensure you have the following overridden methods:

public void onPause() {
    myGlSurfaceView.onPause();
}

public void onResume() {
    myGlSurfaceView.onResume();
}

Anything you hold outside the GL environment will still need to be preserved and restored manually however (bitmaps, game state, etc), for these you'll need to use static fields or a mechanism like SharedPreferences.

seanhodges