Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems running Android app for second+ time
    primarykey
    data
    text
    <p>I am new to android and developing a small game based on AndEngine (www.andengine.org).</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>Thanks, Steve</p> <p>P.S. Same behaviour when run in emulator.</p> <p>UPDATE:</p> <p>I investigated my code further:</p> <p>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.</p> <p>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.</p> <p>My main activity:</p> <pre><code>public class LineTest extends BaseGameActivity { @Override public Scene onLoadScene() { scene = new Scene(1); myLine = new MyLine(); myLine.displayLine(); return scene; } } </code></pre> <p>The MyLine class:</p> <pre><code>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); } } </code></pre> <p>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.</p> <p>I found that two options work to resolve this:</p> <p>Either:</p> <pre><code>Scene SCENE = LineTest.scene; </code></pre> <p>Or:</p> <pre><code>static Scene SCENE; </code></pre> <p>And in constructor:</p> <pre><code> SCENE = LineTest.scene; //I could also pass scene as param to constructor which may be better OOP practice. </code></pre> <p>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.</p> <p>Is there a rule of thumb I can use when deciding what type variables (and methods) should be?</p> <p>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! {:-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload