Search code examples
androidlibgdx

LibGDX: Keep textures (in memory) when switchting between screens


I am programming a LibGDX game where I switch between my menu screen and my game screen very often. If i didn't manually call the

dispose()

method of the screen, the textures would not get disposed and after a while my app would crash. So I call the dispose() method before I set the new screen.

Now to my question: How can I keep the textures even if switching screens?

Because right now I dispose everything as I set another screen only to load it again the next time the screen is shown. How can I keep the textures ready all the time without disposing them and without generatin a new one each time?

Thanks in advance!


Solution

  • You need to use an AssetManager. By loading the atlas in the assetManager you can easily get access to textures without the need to reload them every time you instantiate the screen. See https://www.gamedevelopment.blog/asset-manager-libgdx-tutorial/