0

In the Managing your assets is said that the following coded will cause issues public static AssetManager assets = new AssetManager(); But i do not understand the difference between above code and the next code:

public class AssetSingleton {    
    private static final AssetSingleton instance = new AssetSingleton();

    private final AssetManager assets = new Assets();

    public AssetManager getAssets(){
        return assets;
    }
}

...........................................

AssetManager manager = AssetSingleton.instance.getAssets();

So why the first code is unsafe when pausing/resuming the android app? Where and in what way i have to store the AssetManager instance? There is no example in the LibGDX docs. In book "Learning LibGDX Game Development 2nd Edition" there is used singleton with the instance of assetmanager and on resume/pause this singleton loads/unloads resources.

Also is said in Managing your assets:

If you don't set the AssetManager as shown in the last snippet, the usual managed texture mechanism will kick in, so you don't have to worry about anything.

Do i understand correctly: if ihave no singleton and i do not use the static instance of AssetManager - the assets will be loaded and unloaded by LibGDX automatically. If so - how? Whats the difference?

Community
  • 1
  • 1
anmig
  • 23
  • 4

1 Answers1

0
  1. Having static references to assets is especially bad, because the life cycle of the statics might not be the same as the life cycle the context the resources are created in. Check this answer for detail.

  2. If you want to show Loading Screen while Resuming your game, You've to use

    Texture.setAssetManager(manager);
    

    If you don't set the AssetManager as shown in the above statement, the usual managed texture mechanism will kick in, so you don't have to worry about anything.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • 1
    Thanks for the link. It is great answer. Now i understand about statics. Or in other words about why i should load and unload resources every time the app paused/resumed. But i am still confused with "you don't have to worry about anything". Does that mean that if i don`t use the loading screen - libgdx will reload assets automatically? Or i still must to load/unload on resume/pause? – anmig Jul 15 '17 at 19:22
  • You don't have to worry about anything only you've to make your `AssetManager`, `Texture`, `TextureAtlas` non static – Abhishek Aryan Jul 15 '17 at 19:29
  • 1
    Got it. Thanks a lot! – anmig Jul 15 '17 at 19:31
  • Sorry, but i need to know: must i load/unload on resources on resume/pause even if the resources are non static? – anmig Jul 16 '17 at 11:15
  • you need to bind you texture with your application and on resume you've to load all textures of that application. – Abhishek Aryan Jul 16 '17 at 11:37