3

We have an Augmented Reality app that displays either a video or a 3D model when pointing on a specific marker. This works fine, but as soon as we quit the MediaPlayer activity via the Back-Button, the OpenGL Context seems to get destroyed. The app then just restarts and needs to reload all assets including the 3D model which causes a delay of about 10-15 seconds which we want to prevent. I read already something about setPreserveEGLContextOnPause(true) and put it in our GLSurfaceView (we have a 3.x tablet), but it doesn't seem to do anything (do I need to implement something else to make it work? I barely found usable documentation about it).

I'm not sure where in our app the problem could be, I suppose that somewhere our GLSurface gets destroyed and we dont notice it.

Our code from exiting from the MediaPlayer is this:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {            
        this.finish();

        Intent intent = new Intent(MediaPlayerActivity.this, OpenGLActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }
    return super.onKeyDown(keyCode, event);
}

Any hints how we can preserve the OpenGL context, or quit the MediaPlayer without trashing our main activity?

Lennart
  • 9,657
  • 16
  • 68
  • 84
  • 1
    Have you checked how many separate GL-context your device actually supports? The pro-tip is to create two new test activities & transition to those in `onKeyDown(int, KeyEvent)`, one that doesn't use GL at all, and one that shows a simple `GLSurfaceView`. A media player is something that could very well require its own GL context, forcing the tablet to recycle yours. – Jens Nov 09 '11 at 13:27
  • 1
    So I tried calling a test activity which displays a simple Toast() (instead of the media player) and returning back to the GL-activity. Same result, the GL context is gone. I noticed that he seemingly correct calls *onCreate()* and *onResume()* of the GL-activity, but can't call *onResume()* of the SurfaceView, since it is null. – Lennart Nov 09 '11 at 15:02
  • 1
    Aren't all Activities recreated from scratch on such situation? And once onCreate is called you're supposed to recreate your Views. What you can do is recycle your Renderer, for example, using `Activity.onRetainNonConfigurationInstance()` and re-using it during onCreate() if it's found. There you could store your model at least to prevent the need for reloading it. – harism Nov 09 '11 at 15:21
  • 1
    onRetainNonConfigurationInstance() probably won't work, since onDestroy() is never called. The GLSurfaceView is just lost after resuming from another activity – Lennart Nov 09 '11 at 15:40

1 Answers1

1

To get around that, we have put the MediaPlayer on a SurfaceView layer, that will display over our OpenGL stuff. Depending on the marker, we just show or hide the layer and prevent switching out of context.

Lennart
  • 9,657
  • 16
  • 68
  • 84