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

來源:互聯網
上載者:User

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.螢幕切換時關閉軟鍵盤IME彈出 <activity  ...android:windowSoftInputMode="adjustUnspecified|stateHidden" />  或者在代碼裡面調用關閉IME軟鍵盤的代碼:

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

 附上(開啟IME代碼):

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(關閉IME參考地址)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.