Home » Android » orientation – onCreate not called when screen rotates – Android

orientation – onCreate not called when screen rotates – Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am building an app that needs to know whether the device is in landscape or portrait mode. So I figured, I will use onCreate, I know that is called on device rotation. But onCreate is call only once!! When I rotate the device, nothing happens. I am debugging on an actual device, Motorola Xoom, so I am NOT using the emulator. Someone had this kind of problem?

How to&Answers:

Check if int the Manifest file you declare

android:configChanges=”orientation”

if you declared this flag, the activity will not be destroyed and recreated and the callback

onConfigurationChanged()

will be called.

Answer:

For target API > 13
you need to declare both orientation|screenSize for your Activity to make onCreate not call

<activity 
    ...
    android:configChanges="orientation|screenSize"
    />

If you only define orientation or screenSize, onCreate still call.
Chekc it here activity-element -> android:configChanges -> “orientation”