3

I have an activity with two layout files, one each for portrait and landscape modes. I see that the onCreate method is called each time I change orientation. I tried using

android:configChanges = "orientation"

in the manifest file, but it only reads the portrait layout. Is there a way I can set the layout to the landscape mode without calling onCreate?

Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85

4 Answers4

2

If you use android:configChanges = "orientation", you must override the onConfigurationChanged(...) method of your Activity. Check the newConfig parameter for the new orientation then manually set the layout you want.

See the following links:

onConfigurationChanged(Configuration newConfig)

Handling the Configuration Change Yourself

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • This would mean that I have to save and reuse my activity data anyway. Am I right? – Rameez Hussain Dec 04 '12 at 21:11
  • Not as far as I understand it. Using `android:configChanges = "orientation"` prevents the `Activity` from being destroyed/re-created as the result of an orientation change and `onConfigurationChanged(...)` is called instead. Any other configuration change will, however, result in the `Activity` being destroyed/re-created and `onConfigurationChanged(...)` will not be called. – Squonk Dec 04 '12 at 21:16
  • @RameezHussain : I've added another link to my answer which explains handling the configuration change. – Squonk Dec 04 '12 at 21:26
0

You could create two layout folders. One for portrait mode and one for landscape. layout and layout-land.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
0

This is a fact of life with android. You need to develop your activity around the fact it can be destroyed at any minute due to a configuration change like the screen's orientation changing. Save all the the information used to build the screen in on pause and recreate it in onCreate.

barconr
  • 197
  • 1
  • 11
0

If you want your activity be only in landscape mode, you set this attribute for the activity in the AndroidManifest.xml:

        android:screenOrientation="landscape"

You can put your layout.xml files in such folders for different screen orientations:

  • res\layout-land
  • res\layout-port
ViliusK
  • 11,345
  • 4
  • 67
  • 71