Home » Android » android – onConfigurationChanged not getting called

android – onConfigurationChanged not getting called

Posted by: admin March 10, 2020 Leave a comment

Questions:

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?

How to&Answers:

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+

Answer:

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

android:configChanges="orientation|screenSize"

Answer:

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.

Answer:

check that your device has “Screen rotation” setting ON

"Screen rotation" setting

Answer:

  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.

Answer:

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.

Answer:

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.

Answer:

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"/>

Answer:

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().

Answer:

<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"

Answer:

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.

Answer:

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.

Answer:

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>

Answer:

@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 !

Answer:

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)

Answer:

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.