Search code examples
androidandroid-activityorientation

Android, how to not destroy the activity when I rotate the device?


I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again. How to not destroy the activity?


Solution

  • For API 12 and below: add

    android:configChanges="orientation"
    

    Add "screenSize" if you are targeting API 13 or above because whenever your orientation changes so does your screen size, otherwise new devices will continue to destroy your activity. See Egg's answer below for more information on using "screenSize"

    android:configChanges="orientation|screenSize"
    

    to your Activity in AndroidManifest.xml. This way your Activity wont be restarted automatically. See the documentation for more infos