解決onConfigurationChanged不被呼叫
onConfigurationChanged 最近一直遇到一個很奇怪的問題。那就是我在AndroidManifest.xml的確設定了android:configChanges="orientation“,在我的Activity裡也重寫了onConfigurationChanged。但是同樣的程式碼 在Android 4.0.3的手機裡就不執行onConfigurationChanged。在Android 2.3裡的手機執行一切正常。好幾次去找原因,都因為自己不夠耐心和仔細,而無功而退。直到今天決定慢悠悠地找,終於在這裡找到了答案。
英文原文如下:
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+
我的翻譯:
自從Android 3.2(API 13),screen size也開始跟著裝置的橫豎切換而改變。所以,在AndroidManifest.xml裡設定的MiniSdkVersion和TargetSdkVersion屬性大於等於13的情況下,如果你想阻止程式在執行時重新載入Activity,除了設定”orientation“,你還必須設定"ScreenSize",就像這樣子,android:configChanges="orientation|screenSize"。但是呢,如果你的Target API 級別小於13,你的Activity自己會自動處理這種ScreenSize的變化。如果你的TargetSdkVersion小於13,即使你在Android 3.2或者更高階別的機器上執行程式,它還是會自己去處理ScreenSize的。
更多請參考http://developer.android.com/guide/topics/resources/runtime-changes.html。
我的備註:可能翻譯的不好,反正意思就是,如果你的TargetSdk超過12,然後你想在安裝了Android API level 超過12的裝置上使用onConfigurationChanged,你就必須再對應的Activity里加上對應的screenSize。 一句話答案,把
android:configChanges="orientation" 改成android:configChanges="orientation|screenSize" 就OK了。
另外,StackOverFlow真是一個神奇的網站。
轉自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html
相關文章
- surfaceview元件的surfaceCreated()不被呼叫的解決方案View元件
- 由於navigation引起viewwillappear不被呼叫NavigationViewAPP
- 如何使程式不被360軟體Kill掉?(解決方案)
- android轉屏,切換螢幕,橫豎屏(onConfigurationChanged)會重新 OnCreate 的解決方案Android
- Activity關於onConfigurationChanged方法
- 解決 flutter module 中 .android 和 .ios 目錄不被覆蓋的問題FlutterAndroidiOS
- IOS呼叫相簿顯示英文解決方法iOS
- 解決介面呼叫的provisional headers are shownHeader
- VUE 呼叫 flask 介面,解決跨域問題VueFlask跨域
- php不能呼叫sendmail發信的解決方法PHPAI
- Feign 呼叫丟失Header的解決方案Header
- 【轉】vue mounted 呼叫兩次的解決辦法Vue
- js實現的跨域呼叫flash解決方案JS跨域
- SAP Cloud for Customer的CTI呼叫中心解決方案Cloud
- 完美解決呼叫“sudo rosdep init“命令時的報錯ROS
- 解決直播商城原始碼中,getView被重複呼叫原始碼View
- JavaScript呼叫App原生程式碼(iOS、Android)解決方案JavaScriptAPPiOSAndroid
- 伺服器端解決JS跨域呼叫問題伺服器JS跨域
- 解決linux oracle shell上下箭呼叫歷史命令LinuxOracle
- 資料庫調優教程(十) 【精華章節】解決like ’%str’ 時索引不被使用的4種方法資料庫索引
- Android 原始碼分析之旅3 4 onConfigurationChangedAndroid原始碼
- Android學習筆記——關於onConfigurationChangedAndroid筆記
- SSH在呼叫Service時獲取為空-解決辦法
- 利用 Transform 解決模組化開發服務呼叫問題ORM
- MATLAB呼叫NASTRAN的連續計算解決方案MatlabAST
- 呼叫System.gc沒有立即執行的解決方法GC
- ajax呼叫,action返回的中文為亂碼的解決方案
- 使用Promise解決多層非同步呼叫的簡單學習Promise非同步
- Filter+Redis解決專案之間呼叫的冪等性FilterRedis
- [解決] spring service 呼叫當前類方法事務不生效Spring
- React 中 呼叫 lodash.debounce 不錯誤繫結 this 解決方案React
- 12355青少年服務檯呼叫中心解決方案
- Excel表格中保護公式不被修改Excel公式
- Action呼叫字首詳解
- windows10系統開機提示呼叫失敗怎麼解決Windows
- 原生Js呼叫Fetch Api請求介面(新的Ajax解決方案)JSAPI
- 解決spring cloud Feign遠端呼叫服務,新增headers解決攔截器攔截問題SpringCloudHeader
- 基於laravel的呼叫系統,解決企業打電話問題Laravel