0

Basically what the title says. Currently if the orientation changes in my app the ball resets back to the top of the game, however I wish to change my code so that if the user changes orientation of the device the ball and racket stay in the same spot. How would I achieve this? Would I place my entire in these two methods?

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);
Log.v(TAG,  "onSaveInstanceState");
}

and

protected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);
Log.v(TAG,  "onRestoreInstanceState");
}

(note I have already initialized the constants for TAG, within my code not seen here).
(it's also an activity)

user3584935
  • 51
  • 1
  • 9

1 Answers1

0

You could the save the position of the ball and racket in the outstate so in the activity:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    //If it is an integer
    outState.putInt(TAG, mBallPosition);

    //Or use a serializable if you wish to store an actual object
   outState.putSerializable(TAG, mBallPosition);
}

Then in your onCreate method you can restore the states :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        int position = savedInstanceState.getInt(TAG,0);
        //set ball position

        //or get the object back using outstate.getSerializable(TAG) 

    }
}
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56