The right way to recycle screens in Libgdx

What is the correct way to completely remove a screen in Libgdx? Currently, if I press the button where the button was on my previous screen, the button still does what it would do if I were on this screen. Should I be .dispose() - all that I can in the dispose() method? or is there an easier way to get rid of everything on the screen?

+7
java android dispose libgdx
source share
3 answers

Unfortunately, there is no simpler way. These classes do not use the generic " Disposable " interface or anything like that to do this automatically. Everything that the dispose() method has must be removed manually when it is no longer needed.

This is true for Screens . When switching Screens they are not automatically deleted, but you need to do it yourself (before calling Game.setScreen() ).

On the other hand, it does not really matter. Just browse through your Screen and see if you need to dispose of it or not. If there is a dispose method, name it in dispose() Screen .

BUT this does not explain your behavior regarding the invisible buttons from the last Screen . Suppose you use Stage and use Gdx.input.setInputProcessor(stage); . This parameter will not be changed when the screen changes, and you must set the input processor to the Stage your current Screen or to what processes the input in the current Screen . Thus, the "old" scene will no longer capture any inputs.

+17
source share

I can confirm that this problem does not pass the new stage to the inpur processor. this will bring up the ghost buttons as described.

+2
source share

Unfortunately LibGDX API Documentation says

Note that dispose () is not called automatically.

So, I am doing the removal of all disposable devices (e.g. Stage , Skin , Texture ... etc.) inside the hide() method on the screen because hide() is called automatically and works very well!

Example:

 public class GameScreen implements Screen { ... @Override public void hide() { mainStage.dispose(); playGroundStage.dispose(); controller.dispose(); labelActor.dispose(); } ... } 
0
source share

All Articles