how to create loading screen in libgdx?

android, libgdx, opengl-es

Solution

As suggested in the comments, the AssetManager is the way to load most libGDX resources (audio, textures, etc) asynchronously while showing a splash or loading screen.

For other operations, running them in a background thread (or using one of the other Android or Java background task execution facilities) should be sufficient. To invoke libGDX routines like `setScreen` or others that need to be executed on the libGDX render thread, use `Gdx.app.postRunnable`, like this:

Gdx.app.postRunnable(new Runnable() {
     @Override
     public void run() {
         // Do something on the main thread
         myGame.setScreen(postSplashGameScreen);
     }
  });

Depending on the visibility of `myGame` and `postSplashGameScreen` it may be easier to construct the Runnable in a different context and then pass it over to the background thread to post when its done.

Problem

I have a `GameScreen` class that renders my game. but before starting to render the game, it needs to reading files and initializing that is time consuming. So I need to show/render another `Screen` class called `LoadingScreen` in order to spending some time and concurrently read my files and do initializing process for my `GameScreen`, and after initializing completed changing the screen by calling `setScreen(gameScreen)`. I need to use thread for making this concurrent work, now the problem is that if I use a thread to read files and initializing; When switching to the `GameScreen` the openGl gives me this error: ``` javax.media.opengl.GLException: Error: no OpenGL buffer object appears to be bound to target 0x8892 at com.sun.opengl.impl.GLBufferSizeTracker.setBufferSize(GLBufferSizeTracker.java:118) ``` I am aware of not both of threads use the graphic resources simultaneously. I have found that the problem causes with `Mesh`es. Initializing a Mesh in initializer thread and rendering in main thread causes this error. But I don't know how to solve it. Do you have any ideas to solve this problem?

Original source