Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreate not called when screen rotates - Android

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?

like image 670
Sandra Avatar asked Mar 14 '12 11:03

Sandra


People also ask

Is OnCreate called on orientation change?

Notice that Android does not call onCreate and onDestroy because we retained the Fragment; nor does it call the constructor, because the same Fragment instance will be used after the orientation change.

Is OnCreate only called once?

OnCreate is only called once.

How do I stop Android from restarting activity when changing orientations?

Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.


2 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.

like image 145
Blackbelt Avatar answered Sep 16 '22 14:09

Blackbelt


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"

like image 21
Linh Avatar answered Sep 18 '22 14:09

Linh