2

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:

  1. Having the GLSurfaceView or the Activity see if the game is over by polling a flag on the Renderer, but there is no loop on those classes, (and the onDraw on the GLSurfaceView is never called)
  2. Using the queueEvent() method, but the Renderer class doesn't have it (and using GLSurfaceView.queueEvent() doesn't work because the Runnable remains of the Renderer thread)
  3. 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?

capitano666
  • 656
  • 1
  • 9
  • 24

1 Answers1

2

You shouldn't be calling setContentView() multiple times.

You could instead use multiple activities, calling finish() on your game activity when you're done with it to return to the static view Activity.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Thanks, calling finish() just stops everything and I don't have to care at all about multi-threading, it also looks smoother on transition ;) – capitano666 Mar 06 '11 at 21:07
  • cool! Consider marking accepting answers using the green checkmark. It benefits both you and the answerer and encourages people to answer your questions in the future. – Matthew Mar 06 '11 at 21:51