onConfigurationChanged not getting called
AndroidAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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+
Solution 2 - Android
Some devices of 4.0 doesn't call onConfigurationChanged. Just add a listener to screenSize too.
android:configChanges="orientation|screenSize"
Solution 3 - Android
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.
Solution 4 - Android
check that your device has "Screen rotation" setting ON
Solution 5 - Android
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.
Solution 6 - Android
- Check that you are not using
android:screenOrientation
in anActivity
or in aApplication
level. - Try using
android:configChanges="orientation|keyboardHidden"
instead.
Solution 7 - Android
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.
Solution 8 - Android
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
https://stackoverflow.com/questions/9566305/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"/>
Solution 9 - Android
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.
Solution 10 - Android
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().
Solution 11 - Android
<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"
Solution 12 - Android
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>
Solution 13 - Android
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.
Solution 14 - Android
@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 !
Solution 15 - Android
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)
Solution 16 - Android
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()
Solution 17 - Android
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.