I'm working on a small game and I'm having problems changing my Activity's content view when a GLSurfaceView is set, let me describe how the app works:
The app has only one Activity, it starts with a static view: setContentView(R.layout.main);
, when the user sends a certain input using menus a GLSurfaceView
is instantiated and set via setContentView(gameSession);
(where gameSession is the class extending GLSurfaceView). The GLSurfaceView class then sets the Renderer on which the real application logic runs.
What happens now is that the game logic (ran inside the Renderer
) is the one responsible for knowing when the game is over and the view should change back to R.layout.main
, the Renderer then calls a synchronized
method on the GLSurfaceView, which notifies the Activity to be changed (again with setContentView(R.layout.main);
).
And here comes the problem, as soon as setContentView(R.layout.main);
is called everything hangs, the GLSurfaceView is still there (just not getting updated anymore). I fear that I'm experiencing a deadlock, with the Activity
waiting for the Renderer
to be done before removing it.
I've been thinking on a few solutions but all of them bring other issues:
- Having the
GLSurfaceView
or theActivity
see if the game is over by polling a flag on the Renderer, but there is no loop on those classes, (and theonDraw
on theGLSurfaceView
is never called) - Using the
queueEvent()
method, but theRenderer
class doesn't have it (and usingGLSurfaceView.queueEvent()
doesn't work because theRunnable
remains of the Renderer thread) - Implementing some kind of message thread only to check for game end, but that sounds like a waste of resources.
Has any one of you experienced a similar issue? What's the best way to change your Activity
's content view from a GLSurfaceView
to something else if the logic for when to swap is on the Renderer
?