Q:

LibGDX - How to do more things asynchronously

Most documentation only mentions how to load assets asynchronously, eg with code similar to
private void render(float delta){
  if (assetManager.update()){
     // ** HERE ** //
     game.setScreen(new MainGameScreen(game));
  } else {
    progress = assetManager.getProgress();
  }

  // draw progress bar or whatever 
}

but what if, as part of game initialization, I have to do other things before switching to the main game screen (marked with ** HERE ** in the above code)? Sure, the user is still seeing the splash screen, but what if I wanted to do these things in background as well, and give some sign of progress (another progress bar, or whatever). I haven't found examples of similar things (or perhaps I haven't looked hard enough).

A:

You can use the asynchronous executor, AsyncExecutor, to run things asynchronously in libGDX.
class MyScreen extends Screen {
  private AsyncExecutor executor = new AsyncExecutor(4);
  private AsyncResult<Void> task;
  private float someProgressValue;

  public void create() {
     assetManager.load(...);
     task = executor.submit(new AsyncTask<Void>() {
       public Void call() {
          // Do something here and update someProgressValue as you go
          return null;
       }
     }
  }
}

private void render(float delta){
  // Here check for both the assetManager and the task's completion state
  if (assetManager.update() && task.isDone()){
     game.setScreen(new MainGameScreen(game));
  } else {
    progress1 = assetManager.getProgress();
    progress2 = someProgressValue;
  }

  // draw progress bar for progress1
  // draw progress bar for progress2
}

