onConfigurationChanged not getting called

77,761

Solution 1

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 2

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 3

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

android:configChanges="orientation|screenSize"

Solution 4

check that your device has "Screen rotation" setting ON

"Screen rotation" setting

Solution 5

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.

Share:
77,761

Related videos on Youtube

lblasa
Author by

lblasa

Updated on February 23, 2022

Comments

  • lblasa
    lblasa over 1 year

    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?

    • Pieter Witvoet
      Pieter Witvoet over 12 years
      I'm having the same problem for android:configChanges="keyboardHidden" - onConfigurationChanged is not getting called when the soft keyboard slides in or out.
    • nmr
      nmr almost 12 years
      keyboardHidden is not triggered by the software keyboard, only by hardware keyboards, like the sliding keyboard on the Droid.
    • Ektos974
      Ektos974 almost 10 years
      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.
    • Manuela
      Manuela about 6 years
      @Ektos974 great! adding screenSize gets calling onConfigurationChanged!
    • Manukumar
      Manukumar over 4 years
      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?
  • lblasa
    lblasa over 12 years
    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
    lblasa over 12 years
    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.
  • stoefln
    stoefln almost 12 years
    yeah, that works, but what if I do not want that my app is rotated? i want it to stay in landscape mode.
  • Anuj
    Anuj about 11 years
    wow, just a single attribute to be added and damn thats all, +1 from my side "screenSize" it was.
  • nmr
    nmr about 11 years
    @philipp What would setting the orientation to sensor achieve? Not sure how that would be helpful. Thanks for the edit though.
  • Qadir Hussain
    Qadir Hussain almost 11 years
    you must specify the <activity android:name=".MyActivity" android:screenOrientation="landscape " > </activity> in your menifest file
  • Tobrun
    Tobrun almost 11 years
    The problem you are having is the missing atribute: screensize. Please update your answer. This will be a misleading "fix" for beginning andriod developers
  • aLearner
    aLearner almost 11 years
    @user1281750 Thanks but as a beginning Android developer myself I'm not sure what it is you're exactly saying.
  • AgentKnopf
    AgentKnopf over 10 years
    @aLearner I think what user1281750 meant: This is not actually a fix! What if you need a higher sdk version?
  • aLearner
    aLearner over 10 years
    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.
  • Sampath Kumar
    Sampath Kumar over 10 years
    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
  • pocorall
    pocorall about 10 years
    I set android:targetSdkVersion="14" because Play store requires targetSdkVersion is larger than 11 for tablet support. Is there any workaround for lowering targetSdkVersion?
  • aLearner
    aLearner about 10 years
    @pocorall No, sorry - not that I'm aware of.
  • Dharmendra
    Dharmendra about 10 years
    I had this funny issue...:D
  • Mark Molina
    Mark Molina almost 10 years
    But how do you rotate then?
  • aLearner
    aLearner over 9 years
    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.
  • james82345
    james82345 over 9 years
    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.
  • slycrel
    slycrel about 9 years
    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.
  • Daniel Wilson
    Daniel Wilson over 8 years
    4 years later people still get bitten by this :)
  • Bingchean
    Bingchean over 8 years
    how can i use android:screenOrientation and i want to onConfigurationChanged method run , what can i do,thx
  • Srinivasan
    Srinivasan almost 8 years
    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.
  • suku
    suku almost 8 years
    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 manifest
  • lagos
    lagos almost 8 years
    Thanks. Spent 30 minutes hitting my head against the wall.
  • Pranoy Sarkar
    Pranoy Sarkar about 7 years
    I am also doing the same ,Thanks you saved me
  • Laurent
    Laurent about 6 years
    Ho Thanks dude !
  • Anbarasu Chinna
    Anbarasu Chinna almost 6 years
    This is the perfect solution
  • Vucko
    Vucko about 5 years
    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?
  • Jantzilla
    Jantzilla about 5 years
    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
  • Manukumar
    Manukumar over 4 years
    Is any way to avoid the activity to restart without removing windowNoTitle ?
  • Justin
    Justin about 3 years
    This was a solution for me on 5.0.1.
  • Firzen
    Firzen almost 3 years
    Thank you! This is totally valid. However, I have noticed that there are some situations when not even onCreate() is called :-/
  • LoveMeSomeFood
    LoveMeSomeFood over 2 years
    could you please explain why this is preventing orientation changes ? I'm trying to understand