How to avoid restarting activity when orientation changes on Android

There are various ways to do it, but as given here, using

android:configChanges="keyboardHidden|orientation|screenSize"

allows you to listen for the config changes. You then respond to these changes by overriding onConfigurationChanged and calling setContentView.

This is the way I've been doing it, but I'd be interested to know other people's thoughts.


Define your activity in the AndroidManifest.xml like this:

   <activity
        android:name="com.name.SampleActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:icon="@drawable/sample_icon"
        android:label="@string/sample_title"
        android:screenOrientation="portrait" >
    </activity>

Check in your android manifest file that you have written android:configChanges="orientation" on the activity..


Add android:configChanges="keyboardHidden|orientation" to your activity