For some reason, I need to restart a GLSurfaceView.Renderer so I want some time in my app to call setRenderer(newRenderer) a second time, which Android doesn't like and throws a IllegalStateException saying "setRenderer has already been called"...
Now, I know this is because the renderer is attached to the GLSurfaceView and I need to unbind this renderer to the surface view, so I can call again setRenderer.
Anyone has a solution for this ?
P.S.: Code looks like this :
render = new Renderer(this);
setContentView(R.layout.main);
graphicView = (GLSurfaceView) this.findViewById(R.id.graphicView);
//DO STUFF
graphicView.setRenderer(render);
//DO STUFF
Renderer newRender = new Renderer();
graphicView.setRenderer(newRender); <= ...and Android hates this line sooo much
Thanks !
Found the answer here, this "LemonDev" do saved my life :
One can stop/destroy an OpenGL surface view and set the new GLSurfaceView a new renderer by calling the OnPause() and postDelayed the destruction with a Handler and a Runnable, like this :
final Renderer newRender = new GRenderer(this);
final LinearLayout ll = (LinearLayout) this.findViewById(R.id.main);
graphicView.onPause();
Handler handler = new Handler();
class RefreshRunnable implements Runnable{
public RefreshRunnable(){
}
public void run(){
ll.removeView(findViewById(R.id.graphicView));
GLSurfaceView surfaceview = new GLSurfaceView(getApplication());
surfaceview.setId(R.id.graphicView);
surfaceview.setRenderer(newRender);
ll.addView(surfaceview);
graphicView = (GLSurfaceView) findViewById(R.id.graphicView);
}
};
RefreshRunnable r = new RefreshRunnable(this);
handler.postDelayed(r, 500);
This will create a new renderer and pause the GLSurfaceView. The application then will "destroy" some internal stuffs that you can't touch with the SDK... After this, you can destroy the GLSurfaceView, create another one and set the new renderer to it.
My problem now is that the textures are not refreshed yet but, still, this is great !
Do you really need to call setRenderer twice?
The android documentation mentions :
This method should be called once and only once in the life-cycle of a GLSurfaceView.
Instead, I guess you want to pause the rendering?
Use setRenderMode(RENDERMODE_WHEN_DIRTY);
to pause the renderer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With