IT

onConfigurationChanged가 호출되지 않음

lottoking 2020. 6. 23. 07:06
반응형

onConfigurationChanged가 호출되지 않음


오늘 아침에 나는 onConfigurationChanged이벤트 를 처리하려고하는 문제를 생각 해냈다 . 문제는 휴대 전화의 방향을 변경할 때 재정의하는 메서드가 호출되지 않는다는 것입니다. 전혀 전화하지 않습니다.

나는 android:configChanges="orientation"안드로이드 문서에 언급 된 것처럼 매니페스트에 정의 된 활동을 입었지만 아무런 차이가 없습니다.

이 문제를 생각해 보셨습니까?


이것은 ~ 같은 문제에 대한 나의 gremlin이었습니다.

주의 : Android 3.2 (API 레벨 13)부터는 기기가 세로 방향과 가로 방향으로 전환 될 때 "화면 크기"도 변경됩니다. 따라서 API 레벨 13 이상 (minSdkVersion 및 targetSdkVersion 속성에 의해 선언 된)으로 개발할 때 방향 변경으로 인한 런타임 재시작을 방지하려면 "orientation"값 외에 "screenSize"값을 포함해야합니다. 즉, android : configChanges = "orientation | screenSize"를 데칼해야합니다. 그러나 애플리케이션이 API 레벨 12 이하를 대상으로하는 경우 활동은 항상이 구성 변경 자체를 처리합니다 (이 구성 변경은 Android 3.2 이상 디바이스에서 실행중인 경우에도 활동을 다시 시작하지 않습니다).

( http://developer.android.com/guide/topics/resources/runtime-changes.html에서 )

TL; DR : API 레벨 13 이상을 타겟팅 할 때 configChanges에 "| screenSize"추가


문제는이 방법을 사용하면

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

방향 Activity을 세로 모드로 설정하려는 경우 orientation변경 사항 을받을 수 없습니다 .

따라서 해결책은 setRequestOrientation특정 모드 가 아닌 것입니다. 대신에SCREEN_ORIENTATION_SENSOR.


4.0의 일부 장치는 onConfigurationChanged를 호출하지 않습니다. screenSize에도 리스너를 추가하십시오.

android:configChanges="orientation|screenSize"

기기에 "화면 회전"설정이 켜져 있는지 확인

"화면 회전"설정


  1. 또는 레벨 에서 사용 android:screenOrientation하고 있지 않은지 확인하십시오 .ActivityApplication
  2. android:configChanges="orientation|keyboardHidden"대신 사용해보십시오 .

왜 작동하지 않는지 알아 내기 위해 수십 분을 보냈습니다. 추가 screenSize했지만 여전히 작동하지 않습니다.

내가 가진 요소가 아닌 요소에 추가 된 android:configChanges것으로 나타났습니다 !<application><activity>

물론 이것은 나의 실수 였지만 우리 모두는 이런 바보 같은 실수에 많은 시간을 보낸다는 것을 알고 있습니다. 그래서 나 같은 다른 바보 프로그래머가 있어야하는 경우를 대비 하여이 대답을 추가하고 있습니다.


Macarse는 두 번째 옵션으로 100 % 돈을 벌고 있습니다.

시험 android:configChanges="orientation|keyboardHidden|screenSize"

정확히 같은 문제가 있었고 1.6 에뮬레이터 에서 회전 중에 keyboardHidden원인 onConfigurationChanged추가 되었습니다. 제거하면 호출이 중지됩니다.


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

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


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

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.


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.


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>

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


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)


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.

참고 URL : https://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting-called

반응형