vendredi 9 janvier 2015

Disposal / Initialization of singleton classes in libGDX Android application


Vote count:

0




I have 2 questions regarding the singleton memory management / initialization procedure for my libgdx 1.5.2 based application, when running on an Android device.


[Q1] My application switches between various screens, all screens need access to the same singletons classe (e.g. assets). With respect to singletons I'm not sure what happens with the memory (on Android) when the user switches to a different application and then later resumes the app. Is it necessary to reinitialize the singleton classes every time the application is resumed?


[Q2] Also I notice in lots of examples that singleton classes are disposed in the dispose method (this methods is called by the libgdx Screen class, when it should release all its resources). I can imagine this is important if the singletons are no longer needed, for example when switching screens. However in my specific case all screens need access to the same singletons classes, singleton initialization does not depend on the specific Screen. Can I therefore leave out the disposal of singleton classes int the dispose method?


Starting point of the application


This is where I currently initialize my singleton classes, this class is the starting point from the game, from here the first screen is loaded.



public class MyGame extends Game {
@Override
public void create() {
Gdx.app.setLogLevel(Const.LOGLEVEL);
initSingletons();
setScreen(new StartupView(this)); // StartupView derives from View
}

private void initSingletons() {
Assets.instance.init(new AssetManager());
}
}


Abstract View class


The application switches between various screens, all screens (views) derive from this class, hence I could consider disposing / initializing the singleton classes here. Note that all my so called "view clases" derive from abstract class View and implement the libgdx Screen class.



public abstract class View extends InputAdapter implements Screen {
protected Game mGame;

public View(Game aGame) {
mGame = aGame;
}

@Override
public void resume() {
// should I init singletons here ??
}

@Override
public void dispose() {
// should I dispose singletons here ??
}
}


asked 26 secs ago

Gio

605






Disposal / Initialization of singleton classes in libGDX Android application

Aucun commentaire:

Enregistrer un commentaire