0

I am building a simple android game that I would like to work in both portrait and landscape views. I have created separate xml files in layout and layout-land. In my manifest file I have android:configChanges="orientation|screenSize|keyboardHidden".

My problem occurs when a screen rotation happens during a game. There are some images that begin the game as invisible and then become visible during the game play. If a screen orientation changes during game play, all the attributes of these images reset back to their default setting in the xml file as invisible so now the game becomes unplayable because all of sudden images disappear.

My question is: how do I keep the updated attributes for all my views when a screen orientation occurs?

Additionally, I have had to force the activity to start up correctly by adding this in the onCreate method:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)                {
        setContentView(R.layout.activity_main);
    }else {
        setContentView(R.layout.activity_main_land);
    }

and also manually added onConfigurationChanged method to force a correct display on orientation change. Do I have to do all these attribute updates manually in here?

Ted Petrou
  • 59,042
  • 19
  • 131
  • 136

1 Answers1

0

You can save your game state with onSaveInstanceState() method and restore with onRestoreInstanceState()

See:

Igor Morais
  • 645
  • 1
  • 8
  • 13