161

This morning I came up with a problem trying to handle the onConfigurationChanged event. The problem is that the method, which I override, is not getting called when I change the orientation of the phone. Not getting called at all.

I've put android:configChanges="orientation" on the activity defined in the manifest as mentioned on the android documentation, but this don't make a difference.

Have you come up with this problem?

Macarse
  • 91,829
  • 44
  • 175
  • 230
lblasa
  • 6,284
  • 4
  • 27
  • 29
  • 1
    I'm having the same problem for android:configChanges="keyboardHidden" - onConfigurationChanged is not getting called when the soft keyboard slides in or out. – Pieter Witvoet May 24 '11 at 07:17
  • keyboardHidden is not triggered by the software keyboard, only by hardware keyboards, like the sliding keyboard on the Droid. – nmr Jan 18 '12 at 20:24
  • 12
    You should add "screenSize" in `android:configChanges` This is what the google javadoc said: `Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.` – Ektos974 Jan 27 '14 at 16:59
  • @Ektos974 great! adding screenSize gets calling onConfigurationChanged! – Manuela Sep 18 '17 at 12:40
  • Hi, I'm using Theme.Holo.Light.NoActionBar theme. For this one, my activity is restarting. Is there any way to avoid the activity to get restart? – Manukumar Apr 30 '19 at 14:51

18 Answers18

258

This was my gremlin for the ~same problem:

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

(From http://developer.android.com/guide/topics/resources/runtime-changes.html)

TL;DR: add "|screenSize" to configChanges when targeting API level 13+

nmr
  • 16,625
  • 10
  • 53
  • 67
44

Some devices of 4.0 doesn't call onConfigurationChanged. Just add a listener to screenSize too.

android:configChanges="orientation|screenSize"
Corbella
  • 1,791
  • 14
  • 24
  • This solved my problem. Overriding the `onConfigurationChanged`, testing against `Configuration.ORIENTATION_LANDSCAPE`, and including ONLY `android:configChanges="orientation"` in the manifest was not enough for my Nexus 7 tablet. Thanks – Jantzilla Nov 07 '18 at 15:42
  • This was a solution for me on 5.0.1. – Justin Sep 05 '20 at 07:51
42

The problem was that if you use this method

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

to force the orientation of your Activity to portrait mode, you're not candidate to receive orientation changes.

So the solution is to not setRequestOrientation to a particular mode. But instead use SCREEN_ORIENTATION_SENSOR.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
lblasa
  • 6,284
  • 4
  • 27
  • 29
  • 5
    yeah, that works, but what if I do not want that my app is rotated? i want it to stay in landscape mode. – stoefln Dec 10 '11 at 14:27
  • you must specify the in your menifest file – Qadir Hussain Dec 27 '12 at 07:01
  • 4
    How do I both use this (when user clicks "fullscreen" on a video player), AND keep receiving `onConfigChanged` too, when the user rotates his phone. Same behavior as in youtube app. This possible? – Vucko Aug 14 '18 at 12:27
32

check that your device has "Screen rotation" setting ON

"Screen rotation" setting

Dmitry Kolesnikovich
  • 669
  • 2
  • 15
  • 33
  • 7
    I had this funny issue...:D – Dharmendra Oct 02 '13 at 17:11
  • Is it possible to rotate the activity even though the "Screen rotation" option is OFF?. If YES, please tell me how to do that......?As per, my understanding it will not work but some application rotate the activity to landscape. – Srinivasan Dec 15 '15 at 13:55
  • 4
    S**t!!! Spent 4-5 hours trying to get my code to work! A way to override this, as I learn from another answer is to include in the manifest – suku Jan 17 '16 at 11:42
  • 1
    it is fullSensor and screenorientation nowadays... – Bruno Jennrich Oct 06 '22 at 12:46
24

I spent tens of minutes to find out why it did not work. I added screenSize but it still did not work.

It turned out that I had added android:configChanges to the <application> element, not to the <activity> element as I should have!

Well, of course, this was my mistake, but we all know that all of us spend a lot of time for this kind of silly mistake. So, I am adding this answer just in case there should be another silly programmer like me.

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
20

Macarse is 100% on the money with his 2nd option.

Try android:configChanges="orientation|keyboardHidden|screenSize"

I had exactly the same issue, and on the 1.6 emulator adding keyboardHidden causes onConfigurationChanged to be called during rotation. Remove it and it stops being called.

Gaucho
  • 1,328
  • 1
  • 17
  • 32
William
  • 20,150
  • 8
  • 49
  • 91
19
  1. Check that you are not using android:screenOrientation in an Activity or in a Application level.
  2. Try using android:configChanges="orientation|keyboardHidden" instead.
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • 1
    how can i use android:screenOrientation and i want to onConfigurationChanged method run , what can i do,thx – Bingchean Jun 09 '15 at 02:27
5

Not sure this is the best place for it, but in encountering this issue - I observed something interesting.

If the onConfigurationChanged() listener is NOT working, then onCreate() is called again each time the orientation is changed.

If the onConfigurationChanged() listener is working, then that method is called instead of the onCreate() when orientation changes.

Gene Bo
  • 11,284
  • 8
  • 90
  • 137
  • This is what I am seeing -- but I don't want to be. And according to the docs it shouldn't be happening this way. So confusing. – slycrel Oct 09 '14 at 19:51
  • Thank you! This is totally valid. However, I have noticed that there are some situations when not even `onCreate()` is called :-/ – Firzen Jan 08 '21 at 20:37
4

I had the same problem - onConfigurationChanged was not called when the device changed orientation despite having android:configChanges="orientation|keyboardHidden" in the manifest file. I used the snipped of code shared by Deva here

orientation is not working in 2.3.3?

to check if onConfigurationChanged was being called. It was not.

After a few hours of experimenting, I realized that I had the following lines in the manifest file

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15"/>

and on changing android:targetSdkVersion="15" to android:targetSdkVersion="8", onConfigurationChanged started being called. So, part of the manifest finally looked like this

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8"/>
Community
  • 1
  • 1
aLearner
  • 1,051
  • 14
  • 30
  • 10
    The problem you are having is the missing atribute: screensize. Please update your answer. This will be a misleading "fix" for beginning andriod developers – Tobrun Jan 30 '13 at 13:40
  • @user1281750 Thanks but as a beginning Android developer myself I'm not sure what it is you're exactly saying. – aLearner Jan 30 '13 at 20:05
  • @aLearner I think what user1281750 meant: This is not actually a fix! What if you need a higher sdk version? – AgentKnopf Jul 04 '13 at 13:16
  • I shared what worked for me. I realize that this may not be 'the' fix. But what's the alternative? What's the 'right' solution? And what's meant by the 'missing `attribute: screensize`'? It might be helpful if more details are provided. – aLearner Jul 05 '13 at 04:58
  • Thanks buddy this solution solved my big tension lol.. I was surfing for an hour to get this... its working in all updated versions as well – Sampath Kumar Jul 15 '13 at 07:29
  • I set android:targetSdkVersion="14" because Play store requires targetSdkVersion is larger than 11 for tablet support. Is there any workaround for lowering targetSdkVersion? – pocorall Sep 02 '13 at 06:10
  • @pocorall No, sorry - not that I'm aware of. – aLearner Sep 02 '13 at 09:35
  • If you're gonna downvote my reply at least consider providing an alternative or some constructive commentary. I already mentioned that I don't understand what @user1281750 wrote and they haven't circled back with details. – aLearner Apr 09 '14 at 06:17
  • Thanks! This was the only solution I could find. If anyone can find a solution that works on Android 2.3+ please post. I can not build on 2.3 with attribute: screensize. – james82345 Jul 30 '14 at 00:30
4

I just found that if you have :

android:screenOrientation="landscape"

in the manifest, onConfigurationChanged() will not be called too...

this may similar to:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

that setting orientation block the onConfigurationChanged().

Nick Jian
  • 457
  • 4
  • 12
3
<uses-sdk android:minSdkVersion="8" android:maxSdkVersion="17" />

Dont use any target sdk versions to make you complication. and for all api levels use this as configuration change listener

android:configChanges="orientation|keyboardHidden|screenLayout"
Sampath Kumar
  • 4,433
  • 2
  • 27
  • 42
2

All solutions do not work util I try to remove my theme activity in Android manifest file. So strange

<activity
        android:name="MyActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@style/MyTheme" --> remove this line
        />



<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
2

None of the suggestions worked for me (I had react native project with this issue), but after hours of debugging I found that if you have this line in the AppTheme of styles.xml

<item name="android:windowIsTranslucent">true</item>

then the app will not rotate.

Matej Ukmar
  • 2,157
  • 22
  • 27
1

@thanhbinh84 Gave me an idea what could be causing this.
Removing <item name="android:windowIsTranslucent">true</item> from my theme in styles.xml fixed it !

0

put

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

in your manifest in activity tag

then add

@Override
public void onConfigurationChanged(Configuration newConfig) {
    //don't reload the current page when the orientation is changed
    Log.d(TAG, "onConfigurationChanged() Called");
    super.onConfigurationChanged(newConfig);
}

call onCreate() in onCofigurationChanged()

Pankaj Talaviya
  • 3,328
  • 28
  • 31
0

11 years later... for me it was emulator instances running API 28 and later by default having "Auto-Rotate" turned "Off"(for some reason). This prevents the screen from rotating.

To fix you have to open the emulator device's control panel and turn it on:enter image description here

Matt.M
  • 1,039
  • 3
  • 14
  • 21
-1

I had same issue and I had both "android:screenOrientation" and "android:configChanges" specified in manifest. When I removed first one, onConfigurationChanged() get called on rotation. Strange but it woks)

Mix
  • 3,081
  • 2
  • 17
  • 14
-2

Have you got android.content.res.Configuration in your import statements? Eclipse can insert imports automatically if you press Ctrl+Shift+O.

If that's missing, the compiler will be unable to recognise that you're legitimately overriding the superclass method and so will throw an error.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
  • Yes, the import is already in place, any ideas? because I'm not getting any error or anything, It's just that's not getting called. – lblasa Apr 11 '11 at 10:39
  • I don't know if this could be related to the fact that all the application is being made in portrait mode. The activity in which I need to handle the onConfigurationChangedEvent is an activity contained within a TabHost which mode is portrait. But anyway, I haven't found any comments of this being a problem for catching that event. I'm really out of ideas. – lblasa Apr 11 '11 at 10:46