Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:57:37+00:00 2026-05-18T09:57:37+00:00

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

  • 0

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! {:-)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:57:38+00:00Added an answer on May 18, 2026 at 9:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help. I'm really new to Android, developing in Eclipse with the
I'm developing an Android application and I have a problem running a new thread
Being new to test based development, this question has been bugging me. How much
I'm new to Eclipse/Android development so I'm hoping this is something basic that I'm
I'm new to Android development and I'm currently going through some tutorials. When I
I'm developing Android 2.2 application and I'm going to use Preference.OnPreferenceChangeListener interface. I've added
I am currently developing android XMPP client to communicate with the Tigase server setup
I've just started developing Android applications. I'm having a little problem with networking. If
New class is a subclass of the original object It needs to be php4
New to javascript/jquery and having a hard time with using this or $(this) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.