onConfigurationChanged not getting called
Asked Answered
F

18

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?

Folkway answered 11/4, 2011 at 10:32 Comment(5)
I'm having the same problem for android:configChanges="keyboardHidden" - onConfigurationChanged is not getting called when the soft keyboard slides in or out.Sarthe
keyboardHidden is not triggered by the software keyboard, only by hardware keyboards, like the sliding keyboard on the Droid.Guillot
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.Already
@Already great! adding screenSize gets calling onConfigurationChanged!Wangle
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?Czech
F
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.

Folkway answered 24/5, 2011 at 10:43 Comment(3)
yeah, that works, but what if I do not want that my app is rotated? i want it to stay in landscape mode.Paddie
you must specify the <activity android:name=".MyActivity" android:screenOrientation="landscape " > </activity> in your menifest filePreshrunk
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?Agrigento
G
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+

Guillot answered 9/9, 2011 at 18:45 Comment(5)
wow, just a single attribute to be added and damn thats all, +1 from my side "screenSize" it was.Marrakech
@philipp What would setting the orientation to sensor achieve? Not sure how that would be helpful. Thanks for the edit though.Guillot
4 years later people still get bitten by this :)Omaomaha
Thanks. Spent 30 minutes hitting my head against the wall.Need
This is the perfect solutionSultan
E
44

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

android:configChanges="orientation|screenSize"
Ecstatics answered 28/2, 2013 at 16:1 Comment(2)
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. ThanksRomona
This was a solution for me on 5.0.1.Cytogenesis
F
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.

Folkway answered 24/5, 2011 at 10:43 Comment(3)
yeah, that works, but what if I do not want that my app is rotated? i want it to stay in landscape mode.Paddie
you must specify the <activity android:name=".MyActivity" android:screenOrientation="landscape " > </activity> in your menifest filePreshrunk
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?Agrigento
A
32

check that your device has "Screen rotation" setting ON

"Screen rotation" setting

Allerie answered 9/2, 2013 at 19:32 Comment(4)
I had this funny issue...:DPsycholinguistics
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.Assimilate
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 <android:orientation="sensor"> in the manifestMaterialist
it is fullSensor and screenorientation nowadays... <activity ... android:screenOrientation="fullSensor" ... >Ticknor
A
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.

Anglicism answered 8/1, 2016 at 5:38 Comment(3)
I am also doing the same ,Thanks you saved meIndene
Ho Thanks dude !Herbie
also true exported like this android:exported="true" if this is false then it shouldn't workVinson
F
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.

Flocculent answered 15/5, 2011 at 0:9 Comment(0)
S
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.
Seow answered 11/4, 2011 at 12:26 Comment(1)
how can i use android:screenOrientation and i want to onConfigurationChanged method run , what can i do,thxMariannemariano
W
6

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.

Wrench answered 2/4, 2014 at 17:53 Comment(2)
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.Stilu
Thank you! This is totally valid. However, I have noticed that there are some situations when not even onCreate() is called :-/Willwilla
A
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"/>
Amboceptor answered 28/11, 2012 at 11:25 Comment(9)
The problem you are having is the missing atribute: screensize. Please update your answer. This will be a misleading "fix" for beginning andriod developersPalliasse
@user1281750 Thanks but as a beginning Android developer myself I'm not sure what it is you're exactly saying.Amboceptor
@Amboceptor I think what user1281750 meant: This is not actually a fix! What if you need a higher sdk version?Distrait
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.Amboceptor
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 wellManhattan
I set android:targetSdkVersion="14" because Play store requires targetSdkVersion is larger than 11 for tablet support. Is there any workaround for lowering targetSdkVersion?Janise
@Janise No, sorry - not that I'm aware of.Amboceptor
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.Amboceptor
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.Purposeless
E
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().

Ernst answered 22/7, 2013 at 8:42 Comment(0)
M
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"
Manhattan answered 13/8, 2013 at 5:37 Comment(0)
D
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>
Doit answered 2/12, 2016 at 8:25 Comment(1)
Is any way to avoid the activity to restart without removing windowNoTitle ?Czech
C
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.

Copland answered 24/1, 2018 at 14:6 Comment(1)
could you please explain why this is preventing orientation changes ? I'm trying to understandTrepang
W
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 !

Wormhole answered 23/6, 2017 at 10:6 Comment(0)
R
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()

Rabbi answered 23/2, 2022 at 7:4 Comment(0)
C
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

Cheetah answered 7/7, 2022 at 19:34 Comment(0)
O
-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)

Oriflamme answered 16/7, 2011 at 10:10 Comment(2)
But how do you rotate then?Catoptrics
Works where ? on your one tested device ?Pufahl
H
-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.

Horodko answered 11/4, 2011 at 10:37 Comment(2)
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.Folkway
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.Folkway

© 2022 - 2024 — McMap. All rights reserved.