Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android onConfigurationChanged(Configuration cfg) 無法觸發問題,androidproguard.cfg

Android onConfigurationChanged(Configuration cfg) 無法觸發問題,androidproguard.cfg

編輯:關於android開發

Android onConfigurationChanged(Configuration cfg) 無法觸發問題,androidproguard.cfg


 1.android:configChanges="orientation|keyboardHidden"的使用 

   當在activity加上android:configChanges="keyboardHidden|orientation"屬性,就不會重啟activity.而只是調用onConfigurationChanged(Configuration newConfig).這樣就可以在這個方法裡調整顯示方式.

在xml文件裡面可以進行配置configChanges也可以在代碼中動態配置 

注意:

   1、不設置Activity的android:configChanges時,切屏會重新調用各個生命周期,切橫屏時會執行一次,切豎屏時會執行兩次
   2、設置Activity的android:configChanges="orientation"時,切屏還是會重新調用各個生命周期,切橫、豎屏時只會執行一次
   3、設置Activity的android:configChanges="orientation|keyboardHidden"時,切屏不會重新調用各個生命周期,只會執行onConfigurationChanged方法

 但是: 1)在版本android:targetSdkVersion<=12屏幕切換的時候才會觸發調用onConfigurationChanged(Configuration newConfig)此方法  

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="12" /> 

2)在版本13+以上的時候必須添加上"|screenSize",即 android:configChanges="orientation|keyboardHidden|screenSize" 才會觸發onConfigurationChanged(Configuration newConfig)此方法

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="13" /> 

2.下面是屏幕切換的代碼:

 

 Configuration cfg=getResources().getConfiguration();

if(cfg.orientation==Configuration.ORIENTATION_LANDSCAPE){
GetSystemInfoActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}
if(cfg.orientation==Configuration.ORIENTATION_PORTRAIT){
GetSystemInfoActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

 

重寫chonConfigurationChanged(Configuration newConfig)方法觸發調用

@Override
public void onConfigurationChanged(Configuration cfg) {
super.onConfigurationChanged(cfg);

Toast.makeText(GetSystemInfoActivity.this, "test:"+(cfg.orientation==Configuration.ORIENTATION_LANDSCAPE?"橫屏":"豎屏"),Toast.LENGTH_SHORT).show();

}

 

 3.屏幕切換時關閉軟鍵盤輸入法彈出  <activity  ... android:windowSoftInputMode="adjustUnspecified|stateHidden" />    或者在代碼裡面調用關閉輸入法軟鍵盤的代碼:

InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);

 附上(打開輸入法代碼):

InputMethodManager inputManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(p, 0);

     

onConfigurationChanged為何不被調用?

 

英文原文如下:


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+

 

參考:

   1.http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html

   2.http://www.cnblogs.com/xiaokang088/p/3540189.html

   3.http://blog.csdn.net/songshimvp1/article/details/50109879

   4.http://www.cnblogs.com/androidez/archive/2013/04/09/3011399.html(關閉輸入法參考地址)

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved