How to destroy and recreate a scene in andengine correctly?

andengine, android, java

Solution

Personally, I would create the scene once the first time it is used.

To change the scene, do your removal stuff as you've shown, I wouldn't bother with the call to System.gc(), and then instead of creating a new Scene() - just call scene.reset(), scene.dosomestuff, etc

Creating a new Scene like you show looks like a major memory leak, or at least a possible leak.

Problem

In andAngine I need to destroy a Scene in andangine and to recreate it in order to restart the game variables and listeners and gamelogic. i use this code: ``` scene.detachChildren(); scene.clearEntityModifiers(); scene.clearTouchAreas(); scene.clearUpdateHandlers(); System.gc(); thisengine.setScene(menuscene); ``` and then I recreate the scene ``` scene = new Scene(); scene.dosomestuff thisengine.setScene(scene); ``` Something seems to go wrong when I recreate the third time the scene. Sprites doesn't display..are distorted or something doesn't display at all. Can anyone explain to me if I correctly initialize and destroy the scene ?

Original source