Question

I am new to android and developing a small game based on AndEngine (www.andengine.org).

The game displays a tiled map for the background, with different types of tiles (some moveable, some rotatable). I also draw some lines on the screen.

The problem is, when the game runs first time (running on phone, started from Eclipse) it runs flawlessly but if I exit the game using the phone's BACK button and restart the game from the phone homescreen icon the application is very buggy. The lines no longer appear on the screen and the functionality of moving and rotating tiles works only once then the game is non-operational. It doesn't crash but the bugs caused by the game restart make it useless.

I've read all I can find on the application lifecycle and tried setting various objects to null in the onDestroy() method but nothing I do has made any difference. Something is clearly 'hanging around' from the first app run and causing issues when it is started a second time. Please help, 24 hrs of Googling and wracking my brain has been fruitless.

Thanks, Steve

P.S. Same behaviour when run in emulator.

UPDATE:

I investigated my code further:

I created a small program to investigate part of the above issue, drawing a line and it not being drawn on the re-run of the app.

Using AndEngine one must Override onLoadScene(). In this method you specify what you want on screen at startup and the method returns a Scene object. A Scene handles what you see on-screen, so if you want to say, add a new line (or sprite, or whatever) to the screen, you call myScene.addEntity(myLine). I created a main activity class and a class called MyLine which draws the line.

My main activity:

public class LineTest extends BaseGameActivity {

@Override
    public Scene onLoadScene() {
        scene = new Scene(1);
        myLine = new MyLine();
        myLine.displayLine();   
        return scene;
    }
}

The MyLine class:

public class MyLine {

    static final Scene SCENE = LineTest.scene;
    static final int LINE_WIDTH = 4;

    Line line = new Line(0,0,0,0);

    public MyLine() {
    }

    public void displayLine() {
        line.setLineWidth(4);
        line.setColor(1f, 0f, 0f);
        line.setPosition(10, 10, 400, 400);
        SCENE.getBottomLayer().addEntity(line); 
    } 

    public void removeLine() {
        SCENE.getBottomLayer().removeEntity(line);
    }
}

You may notice a problem with the above, in my defense I am new to Java and OOP. My activity uses only one Scene, so I thought, within my MyLine class, I could declare SCENE as a static final as it will not change. WRONG! Debugging the program I found that the static final SCENE once set, never changes, even after the program has been stopped (using back key) and restarted. However, when the program restarts the code Scene scene = new Scene(1); creates a new Scene with a new ID, so the static final SCENE points to the old Scene not the new one, hence no line is created in the new scene.

I found that two options work to resolve this:

Either:

Scene SCENE = LineTest.scene;

Or:

static Scene SCENE;

And in constructor:

    SCENE = LineTest.scene; //I could also pass scene as param to 
constructor which may be better OOP practice.

I suspect my other issues with my game application are all related to declaring things as static or static final when they shouldn't be.

Is there a rule of thumb I can use when deciding what type variables (and methods) should be?

UPDATE: I had three variables in my Game class declared as static final when they should have been just static. Changing them to static and assigning them in the constructor has resolved all the problems, WOOHOO! {:-)

Was it helpful?

Solution

Why do you even have a static field in your class when it's specific to the instance? Make it an instance variable or so.

You could also reset all static fields manually when the application gets opened, but that would be very ugly.

OTHER TIPS

Something is clearly 'hanging around' from the first app run and causing issues when it is started a second time.

Have you created your own Application subclass? If you are doing some initialization of the game at the Application level and then other initialization at the Activity level, then the former wont be repeated until the app itself is killed and restarted. Simply exiting using the BACK button doesn't kill the app directly.

Be careful with the use of static final variables!!! Solution to my problems above.

I met this problem too, and I resolve it by remove some static variables and static method. I Know the static variable will stay in memory when click back button in android, but I still don't know why the effect so strange. Also thanks everybody above!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top