I know this question was already asked, but mine is a little different:
I have 2 different layout files for my game; one for portrait mode and one for landscape. When I rotate the screen, the onCreate method restarts my game (creates all the elements again). I don´t want this to happen, so I wrote this line in the manifest:
android:configChanges="orientation"
It works, onCreate is not called, but the new layout is not being showed properly!
I tried to put the following code in my Activity, but it just keeps doing weird things:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.gameview);
}
how can I fix this?
thanx guys
First of all understand how orientation changing in android works:
By default activity restarts on orientation changed event (and goes throw
onCreate).If you write
android:configChanges="orientation"in manifest – it means it will not be recreated, but just remeasure and redraw view tree.Comments about your code:
setContentViewshould called just once per activity lifecycle.General way to handle this situation is:
onSaveInstanceStateonCreatemethod restore game state if it is supplied (savedInstanceStateparam is not null).