Search code examples
androidlibgdxreload

Buttons texture garbled (Libgdx) upon reload


I am developing a game in libgdx. When i load game from background apps it shows as following. What could be the possible cause?

Correct Image

Garbled texture


Solution

  • May be you're using static resources, that is most common reason for this problem.

    When your app is paused, OS might decide to close it (to free memory), including its OpenGL context but it is not guaranteed to do so.

    When your app come in foreground, OS starts a new instance of your app. For that it re-uses the same VM of the previous instance of your app, including all assets it already has loaded. This also means that any static variables will have the value they had from the previous run of your app. If any of those variables include any resources, then those resources won't be valid anymore. Your application create a new OpenGL context, but the assets still point to the old invalid context.

    so I suggest don't keep/use static resource in android e.g. AssetManager, Texture, BitmapFont....

    If you do still decide to use statics then make sure to fully understand their life-cycle.