In Libgdx only one thread (the thread that executes the app lifecycle callbacks) has a valid OpenGL context and can invoke OpenGL calls. You can post a Runnable
to the GDX thread from your other threads to get it to execute stuff on behalf of other threads. Posted runnables will be executed before the next render callback is run. See Gdx.app.postRunnable()
In your case you could do something like this:
@Override
public void received(Connection connection, Object data) {
super.received(connection, data);
nb++;
if (nb == 5) {
final MyGdxGame g = MyGdxGame.this;
Gdx.app.postRunnable(new Runnable() {
public void run() {
g.setSecondScreen();
});
}
}
Depending on how often this happens, you may want to avoid allocating a new Runnable
on each callback, so if you make your MyGdxGame
Runnable
or make a custom Listener
that implements Runnable
you should be able to avoid the allocation.