Question

I have issues with OpenGL ES context loss under LibGDX, so I'm trying to figure out how to solve the problem. My first step was to actually re-initialize all my textures when the resume function is called in one my classes that extends Screen. Like this:

@Override
public void resume() {
    Tile.initTiles();
}

The resume function re-creates all my tiles (including their textures), so I thought this would work. However, according to the documentation: ApplicationListener Docs

The resume function should never be called on a desktop. Now, resume is never called on my Android phone, but on my desktop I tell the program to print "true" to the console in the resume method, and voila, the resume function is actually called on the desktop.

My main questions are:

  • Why would the resume function be called on my desktop but not on my Android phone?
  • How can I reload my textures on resume for my Android phone? Currently my textures are white on resuming the game after hitting the back key. Interestingly enough the textures reload fine when exiting through the home button.
Was it helpful?

Solution

I'll quickly explain how I do it, and for me it worked out so far without problems on both desktop and Android.

First of all I use an AssetManager for all my assets, including Texture. I usually load all my assets before entering the actual gameplay screen via a loading screen. But it should also work when you load them in your Screen.show() method.

Whenever I need an asset, the only way I retrieve them is via AssetManager.get(...). My AssetManager is actually a public static member of the base game class, so it can be accessed from anywhere in my code and there is only a single one of them.

In my Screen.resume() method I put an AssetManager.finishLoading(), though I'm not sure this is really necessary.

Right after the game starts and your AssetManager is instantiated, I call the static method Texture.setAssetManager(...).

This closes the circle. When on Android your OGL context is lost, LibGDX will actually revive it for you. Since you've set the AssetManager for your Textures, the manager will be able to find the textures after they have been reloaded. The AssetManager.finishLoading() will wait until the reloading has been finished. After that, everything should work as before the context loss.

Why Screen.resume() is not called, I cannot say. For me it is called on Android. Maybe you need to update your LibGDX version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top