Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx on desktop, how to handle disposing when window is closed

I'm using libgdx (com.badlogic.gdx.Game and Screens and that stuff) to make a game. I have the following problem: on desktop, when I close the window (from the cross on top), of course the application closes. But I would like to go back to menu screen and dispose there, since I do disposing of resources there (via asset manager). I have buttons for exiting in the game and if exiting is done that way, it's fine. The trouble is that the red cross can be pressed. So how could I handle disposing properly in that case?

On the android version, I catch the back key and handle leaving different parts of the game and the whole game in my own way. And there it works OK.

Another related question I have:

On desktop the application cannot get stopped and then disposed (on it's own, without user explicitly exiting it) like the android version can (the android life cycle), right? So is it a good idea, if I do a temporary save on pause() and restore game state from that on resume(), that I don't restore on desktop (since the restoring isn't a complete restore and I don't want restoring to happen if, on desktop, the window is just minimized (as I noticed, pause()/resume() gets called when minimizing/restoring window )).

Hope this wasn't too unclear :D. I've tried to google for answers but don't seem to find anything. Any help is much appreciated.

like image 794
ploosu2 Avatar asked Apr 08 '14 17:04

ploosu2


1 Answers

I suggest using the libgdx life-cycle methods

To dispose you should use the dispose() method. You don't need to call dispose yourself! It will be called automatically when the application gets destroyed, see documentation:

dispose () Called when the application is destroyed. It is preceded by a call to pause().

So just implement the dispose method in your Screens:

@Override
public void dispose () { 
    //your code needs to get added here, like for example:
    stage.dispose();
    texture.dispose();
    //etc..
}

Update: Note that dispose() of AppliacationListener gets called automatically, not dispose() of Screen, see comment of Springrbua

like image 121
donfuxx Avatar answered Oct 02 '22 09:10

donfuxx