15

On android, the GLSurfaceView documentation says this:

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.

So I'm supposed to do something like this in my activity:

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

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

I'm observing in my code that if I don't call onPause() and onResume() then the context is not lost when I press the home button, so I can switch between applications and then go back to my game and everything is working. What I see is that if I close the game using the back button then the screen is black when I open it again, but I can change the back button behaviour to totally close the game and avoid this problem.

So my question is: when is the OpenGL context destroyed? If I don't call onPause() and onResume() can I assume that it will never be destroyed?

EDIT:

I'm targeting Android 2.2, so setPreserveEGLContextOnPause() is not an option to me.

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
Damian
  • 5,471
  • 11
  • 56
  • 89
  • I'd be curious to know the value of myGlSurfaceView.getPreserveEGLContextOnPause(). If that's set to true, then your context may be preserved. See http://developer.android.com/reference/android/opengl/GLSurfaceView.html#setPreserveEGLContextOnPause(boolean) for more info. – R Hyde Jun 19 '12 at 11:55
  • @RodHyde sorry, I forgot to say I'm targeting Android 2.2 – Damian Jun 19 '12 at 17:05
  • Here is a code that uses GLSurface for drawing. http://code.google.com/p/andengine/source/browse/src/org/anddev/andengine/opengl/view/GLSurfaceView.java?r=ef54dd94f1fe8fa9ed134d993802c3c9974d3a84 – AZ_ Jun 26 '12 at 11:46
  • Did you find a good solution for pre Android 3.0? I could only rebuild the context and reload the textures, this caused either a long blank screen load or sprites to be popping in which looks bad. – james82345 Jun 09 '13 at 04:07

2 Answers2

28

The OpenGL might be lost only after Actvity::onPause() is called, and only in this case. See the setPreserveEGLContextOnPause documentation :

Whether the EGL context is actually preserved or not depends upon whether the Android device that the program is running on can support an arbitrary number of EGL contexts or not. Devices that can only support a limited number of EGL contexts must release the EGL context in order to allow multiple applications to share the GPU. [...] the EGL context [can be] released when the GLSurfaceView is paused, and recreated when the GLSurfaceView is resumed.

EDIT : The situation described in the documentation is valid on all Android version. Not matter you have access to setPreserveEGLContextOnPause

In my opinion, this is one major drawback is Android OGLES implementation : you can't be certain.

The documentation itself is vague (EGL Context Lost note) :

There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep

I noticed the same behavior as you about the Home and Back button. Calls are not exactly the sames (but can't remember them precisely).

The only way to be sure that the OpenGL context is available is to create all OpenGL resources in onSurfaceCreated

Note about setPreserveEGLContextOnPause. Once again, this documentation comment demonstrates the "random" behavior of context destruction :

If set to true, then the EGL context may be preserved when the GLSurfaceView is paused. [...]

rockeye
  • 2,765
  • 2
  • 30
  • 44
  • sorry, I forgot to say I'm targeting Android 2.2 – Damian Jun 19 '12 at 17:06
  • I also work on Android 2.2. In this case, setPreserveEGLContextOnPause is not available, but it does not change the way OpenGL context are lost... – rockeye Jun 20 '12 at 07:10
  • It's still not clear to me. So, if I don't call onPause() of GLSurfaceView and handle the back key to close the app after asking for confirmation, then the context will never be destoyed? – Damian Jun 20 '12 at 19:19
  • More or less : that the way Android (and iOS by the way) works. If you don't completely close your application, it remains in memory to speed up next start up and retrieve the exact same state. However, as specified in the documentation, if other applications creates OpenGL context, Android may destroy the one from your application. About calling OnPause or not, the doc says : *GLSurfaceView clients are required to call onPause()*. It is advised to follow that, otherwise you might have unexpected result given the OS version or the device... but of course, you are free to not call it anyway! – rockeye Jun 21 '12 at 08:15
1

setPreserveEGLContextOnPause is an option for you, you just have to implement the GlSurfaceView yourself.

See my answer here to a similar question: Prevent onPause from trashing OpenGL Context

Community
  • 1
  • 1
arberg
  • 4,148
  • 4
  • 31
  • 39