GLSurfaceView inside fragment not rendering when restarted

Ian Ellis picture Ian Ellis · May 17, 2012 · Viewed 8.9k times · Source

I have a GLSurfaceView set up and rendering as expected using a GLSurfaceView.Renderer. My App uses fragments from the android support package. When I navigate to a new fragment surfaceDestroyed is called but when I come back to the fragment via the backstack the GLSurfaceView will not render, calls to requestRender do not result in an onDraw call.

I am aware that I need to call onResume and onPause on the surface view and I am doing this from the hosting fragment but it doesn't seem to solve the issue. All examples about htis method refer to the activity, could this be the issue? And if so how do you use a GLSurfaceView inside a fragment.

Any insight greatly appreciated, I'm happy to post code but it seems to be more of a general question to me,

Thanks

Answer

Learn OpenGL ES picture Learn OpenGL ES · Feb 22, 2013

Here's how I have my GLSurfaceView setup in a fragment:

onCreateView() {
    glSurfaceView = new GLSurfaceView(getActivity());
   ...
}

onPause() {
    if (glSurfaceView != null) { glSurfaceView.onPause(); }
    ...
}

onResume() {
    if (glSurfaceView != null) { glSurfaceView.onResume(); }
    ...
}

}

So, similar to what you'd do in an activity. This works in my use case, so it seems like they do work in fragments. It's hard to say more without knowing what your code looks like.