How to avoid restarting activity when orientation changes on Android

chirag picture chirag · Dec 31, 2010 · Viewed 63.2k times · Source

I am creating an Android app in which I'm drawing a view on a canvas. When the device's orientation changes, the activity restarts. I don't want it to.

How can I avoid restarting the activity when the orientation changes?

Answer

Mike picture Mike · Feb 8, 2013

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.