'Libgdx TextureAlias returns broken textures after resume

Im creating an android game and started using TextureAlias for game assets, creating it as: TextureAtlas(Gdx.files.internal("assets.atlas")) and getting AtlasRegions simply by atlas.findRegion("my_asset") then setting this region into a sprite. Everything works fine, textures are rendered properly but after I put my app into background (methods pause and hide are called) and then bring it back to front (show is called) my assets are broken, they are grayish, not totally black as they would appear after calling dispose on TextureAlias - what am I missing? Should I be recreating TextureAtlas each time show is called?



Solution 1:[1]

When you as an Android user leave an app, there are 2 things that could happen.

One thing definitely happens, and that's "actually pause", which means the app is still running but not active. If Android needs the resources, it might "actually kill" your app even though you think it's running. (You can search Android App Lifecycle for more info)

In the first case, nothing would happen, your GL context would still exist and everything should work. In the second case, however, your original GL context won't exist anymore, which means the textures would be bust.

The correct solution is to put your TextureAtlas load functionality into the create() function of your Game, so it will be reloaded only if the app was killed and not if the user switched out and in again. You will of course also need to reconstruct the screen so it uses the new textures.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yair Morgenstern