A few things you need to implement when using fragments with glsurfaceview. It's a bit complicated, but stay with me. When you switch to a new fragment, ondestroyview is automatically called onto your fragment using glsurfaceview, which destroys your view (the glsurfaceview that you created and returned to oncreateview).
Therefore, when you move to a new fragment, then onpause, onstop, ondestroyview is called automatically, without any work on your part. when you return to this snippet using glsurfaceview, then oncreateview, onactivitycreated, onstart and onresume are automatically called, without any work on your part.
The key to your question is understanding the fragment life cycle that can be found on the Android developer website, as well as understanding how glsurfaceview works.
Now, using glsurfaceview, you must implement the renderer using onsurfacecreated, onsurfacechanged and ondrawframe. Moving to another fragment and then returning to the fragment using glsurfaceview will cause the onsurfacecreated function to be called again, since your glsurfaceview was destroyed in ondestroyview, your context was lost and you need to reload all your resources in the gl stream.
Finally, from your question you can see that the ondrawframe is not being called, which is probably due to the fact that you are not re-creating your view, not installing the renderer and not returning your view from oncreateview.
so in oncreateview you need something like this
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mView = inflater.inflate(R.layout.fragment_layout, null); surface = (GLSurfaceView) mView.findViewById (R.id.glsurfaceview); surface.setEGLContextClientVersion(2);
You do not want to create your own shader rendering class in oncreateview unless you want your rendering class to “start” every time you return to your fragment using glsurfaceview. Instead, create your rendering class in your onCreate snippets, so if you have something customized, you will start right where you left, because just setting up the rendering will provide you with a surface that will call onsurfacecreated, onsurfacechanged and ondrawframe automatically. Make sure that you reload all the resources that you last used onsurfacecreated in the ondrawframe before drawing them.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); shader = new Shader(this); Log.d("Fragment", "OnCreate"); }
when it comes to pausing and resuming a fragment, then in most cases it is automatically processed when you dynamically replace the fragment and add it to the stack, so just add surface.onpause () and surface.onResume () to the right spots, and you're good to go .
To make things crystal clear, try putting log entries in methods that revolve around the fragment life cycle and glsurfaceview renderer, and you can see what happens and what doesn't happen.