0

My app currently calls a method within the onCreate() method so that the game will start and animations will run once the view is created. However when i flip the screen to switch between portrait and landscape, this method is called again.

I've moved the calling line both to the onStart() method and even the methods class constructor.

this is the method that is being called:

public void startGame() {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        public void run() {
            runGame();
        }
    }, 500);
}

There is a delay to allow everything to be constructed before it is run, otherwise it won't work.

Is there i way to stop onSizeChanged() affecting this method being called? Or is there a way i can call this method so that it starts when the activity is started (again so that onSizeChanged() cant affect it and that everything is initialized before its call).

Thanks for looking.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
Waddas
  • 1,353
  • 2
  • 16
  • 28

3 Answers3

1

The easiest way is to add:

android:configChanges="orientation|screenSize"

which tells the system you'll take care of them yourself. However, it's not recommended.

Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

Or you can check savedInstanceState. If it is not null, that means a user changes the orientation (or the app are coming back from a configuration change). Maybe something likes this:

if(savedInstanceState == null)
    startGame();
else
    //handle a configuration change if necessary

Please see Don't reload application when orientation changes

Edit :

if(savedInstanceState == null) {
    startGame();
} else {
    //handle a configuration change if necessary
    int yourVar = savedInstanceState.getInt("something");
}
Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69
  • That works thank you :) is there a way i can save the state of another class (used by the View class, it creates a sequence and in the constructor it adds 3 integers to the sequence. so when i change the orientation it adds 3 again which i dont want). – Waddas May 01 '13 at 14:03
  • @Waddas i'm not sure what you try to do but you can use onSaveInstanceState. please see http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate – pt2121 May 01 '13 at 14:09
  • Sorry im not being clear. i have a class that does not implement View, is there a way i can save and restore its state so that chnging the orientation does not affect it? – Waddas May 01 '13 at 14:16
  • From my understand, the state should be saved automatically. Anyway, you can override onSaveInstanceState() and add something likes this savedInstanceState.putInt("something", value); Then in onCreate retieve it using savedInstanceState.getInt("something"); – pt2121 May 01 '13 at 14:22
  • please see my updated answer and also see http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate – pt2121 May 01 '13 at 14:25
  • If you are still not sure, could you ask another question with details (Your code) ? it looks like u'r having another problem – pt2121 May 01 '13 at 14:36
  • Ok, thanks for all your help, you've solved my main problem which is a massive help :) – Waddas May 01 '13 at 14:54
  • welcome. i'd be happy to help more but without your code, it's hard to show how to do things. – pt2121 May 01 '13 at 15:00
0

add this to your manifest:

android:configChanges="screenSize"
Royi
  • 745
  • 8
  • 22
0

Could you use a static thread variable instead of handler?

e.g.

  private static Thread gameThread = new Thread(new Runnable(){
        public void run()
        {
              //if necessary: Thread.sleep(500);
              runGame();
        }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
        //rest of oncreate code here
        if(gameThread.isAlive() == false)
        {
              gameThread.start();
        }
  }
Matt
  • 377
  • 4
  • 17
  • Not a good idea. When you initialize the `Runnable` object anonyomously, it will implicitly hold a reference to the parent class(In this case, an `Activity`), and that `Activity` will be leaked as long as the thread keeps running. – Vinay S Shenoy May 01 '13 at 13:57