onConfigurationChanged not getting called
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
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.
Related videos on Youtube
lblasa
Updated on February 23, 2022Comments
-
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 over 12 yearsI'm having the same problem for android:configChanges="keyboardHidden" - onConfigurationChanged is not getting called when the soft keyboard slides in or out.
-
nmr almost 12 yearskeyboardHidden is not triggered by the software keyboard, only by hardware keyboards, like the sliding keyboard on the Droid.
-
Ektos974 almost 10 yearsYou 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 about 6 years@Ektos974 great! adding screenSize gets calling onConfigurationChanged!
-
Manukumar over 4 yearsHi, 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 over 12 yearsYes, 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 over 12 yearsI 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 almost 12 yearsyeah, that works, but what if I do not want that my app is rotated? i want it to stay in landscape mode.
-
Anuj about 11 yearswow, just a single attribute to be added and damn thats all,
+1
from my side"screenSize"
it was. -
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 almost 11 yearsyou must specify the <activity android:name=".MyActivity" android:screenOrientation="landscape " > </activity> in your menifest file
-
Tobrun almost 11 yearsThe problem you are having is the missing atribute: screensize. Please update your answer. This will be a misleading "fix" for beginning andriod developers
-
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 over 10 years@aLearner I think what user1281750 meant: This is not actually a fix! What if you need a higher sdk version?
-
aLearner over 10 yearsI 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 over 10 yearsThanks 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 about 10 yearsI set android:targetSdkVersion="14" because Play store requires targetSdkVersion is larger than 11 for tablet support. Is there any workaround for lowering targetSdkVersion?
-
aLearner about 10 years@pocorall No, sorry - not that I'm aware of.
-
Dharmendra about 10 yearsI had this funny issue...:D
-
Mark Molina almost 10 yearsBut how do you rotate then?
-
aLearner over 9 yearsIf 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 over 9 yearsThanks! 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 about 9 yearsThis 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 over 8 years4 years later people still get bitten by this :)
-
Bingchean over 8 yearshow can i use android:screenOrientation and i want to onConfigurationChanged method run , what can i do,thx
-
Srinivasan almost 8 yearsIs 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 almost 8 yearsS**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 almost 8 yearsThanks. Spent 30 minutes hitting my head against the wall.
-
Pranoy Sarkar about 7 yearsI am also doing the same ,Thanks you saved me
-
Laurent about 6 yearsHo Thanks dude !
-
Anbarasu Chinna almost 6 yearsThis is the perfect solution
-
Vucko about 5 yearsHow 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 about 5 yearsThis solved my problem. Overriding the
onConfigurationChanged
, testing againstConfiguration.ORIENTATION_LANDSCAPE
, and including ONLYandroid:configChanges="orientation"
in the manifest was not enough for my Nexus 7 tablet. Thanks -
Manukumar over 4 yearsIs any way to avoid the activity to restart without removing windowNoTitle ?
-
Justin about 3 yearsThis was a solution for me on 5.0.1.
-
Firzen almost 3 yearsThank you! This is totally valid. However, I have noticed that there are some situations when not even
onCreate()
is called :-/ -
LoveMeSomeFood over 2 yearscould you please explain why this is preventing orientation changes ? I'm trying to understand