Android橫豎屏切換生命週期變化

1. AndroidMenifest沒有設置configChanged屬性。android

豎屏啓動(橫屏啓動相同):測試

onCreate -->onStart-->onResume字體

切換橫屏:.net

onPause ——> onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart -->blog

onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy    生命週期

(Android 6.0 Android 7.0 Android 8.0)事件

總結:沒有設置configChanges屬性Android 6.0 7.0 8.0 系統手機 表現都是同樣的,當前的界面調用onSaveInstanceState走一遍流程,ci

而後重啓調用onRestoreInstanceState再走一遍完整流程,最終destory。get

2.AndroidManifest設置了configChanges   android:configChanges="orientation"it

豎屏啓動:

onCreate -->onStart-->onResume

切換橫屏:

onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart -->

onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy        

(Android 6.0)

onConfigurationChanged-->onPause -->onSaveInstanceState -->onStop -->onDestroy -->
onCreate-->onStart -->onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy        

(Android 7.0)

 onConfigurationChanged    

(Android 8.0)

 
橫屏啓動:

onCreate -->onStart-->onResume

切換豎屏:

onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart -->

onRestoreInstanceState--> onResume -->onPause -->onStop -->onDestroy        

(Android 6.0 )  

onConfigurationChanged-->onPause -->onSaveInstanceState -->onStop -->onDestroy -->
onCreate-->onStart -->onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy        

(Android 7.0)

onConfigurationChanged    

(Android 8.0)

總結:設置了configChanges屬性爲orientation以後,Android6.0 同沒有設置configChanges狀況相同,完整的走完了兩個生命週期,調用了onSaveInstanceState和onRestoreInstanceState方法;Android 7.0則會先回調onConfigurationChanged方法,剩下的流程跟Android 6.0 保持一致;Android 8.0 系統更是簡單,
只是回調了onConfigurationChanged方法,並無走Activity的生命週期方法。
 
3.AndroidManifest設置了configChanges   
android:configChanges="orientation|keyboardHidden|screenSize" 
豎(橫)屏啓動:onCreate -->onStart-->onResume
切換橫(豎)屏:onConfigurationChanged   (Android 6.0 Android 7.0 Android 8.0)

總結:設置android:configChanges="orientation|keyboardHidden|screenSize"  則都不會調用Activity的其餘生命週期方法,只會調用onConfigurationChanged方法。

4.AndroidManifest設置了configChanges   
android:configChanges="orientation|screenSize" 
豎(橫)屏啓動:onCreate -->onStart-->onResume
切換橫(豎)屏:onConfigurationChanged   (Android 6.0 Android 7.0 Android 8.0)

總結:沒有了keyboardHidden跟3是相同的,orientation表明橫豎屏切換 screenSize表明屏幕大小發生了改變,
設置了這兩項就不會回調Activity的生命週期的方法,只會回調onConfigurationChanged 。

5.AndroidManifest設置了configChanges   
android:configChanges="orientation|keyboardHidden" 

總結:跟只設置了orientation屬性相同,Android6.0 Android7.0會回調生命週期的方法,Android8.0則只回調onConfigurationChanged。說明若是設置了orientation 和 screenSize 都不會走生命週期的方法,keyboardHidden不影響。

1.不設置configChanges屬性不會回調onConfigurationChanged,且切屏的時候會回調生命週期方法。
2.只有設置了orientation 和 screenSize 纔會保證都不會走生命週期,且切屏只回調onConfigurationChanged。
3.設置orientation,沒有設置screenSize,切屏會回調onConfigurationChanged,可是還會走生命週期方法。

注:這裏只選擇了Android部分系統的手機作測試,因爲不一樣系統的手機品牌也不相同,可能略微會有區別。
      
另:
代碼動態設置橫豎屏狀態(onConfigurationChanged當屏幕發生變化的時候回調)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

獲取屏幕狀態(int ORIENTATION_PORTRAIT = 1;  豎屏    int ORIENTATION_LANDSCAPE = 2; 橫屏)
int screenNum = getResources().getConfiguration().orientation;

configChanges屬性
1. orientation 屏幕在縱向和橫向間旋轉 
2.keyboardHidden 鍵盤顯示或隱藏 
3.screenSize 屏幕大小改變了 
4.fontScale 用戶變動了首選的字體大小 
5.locale 用戶選擇了不一樣的語言設定 
6.keyboard 鍵盤類型變動,例如手機從12鍵盤切換到全鍵盤 
7.touchscreen或navigation 鍵盤或導航方式變化,通常不會發生這樣的事件
經常使用的包括:orientation keyboardHidden screenSize,設置這三項界面不會走Activity的生命週期,只會回調onConfigurationChanged方法。

screenOrientation屬性
1.unspecified 默認值,由系統判斷狀態自動切換 
2.landscape 橫屏 
3. portrait 豎屏 
4.user 用戶當前設置的orientation值 
5. behind 下一個要顯示的Activity的orientation值 
6. sensor 使用傳感器 傳感器的方向 
7. nosensor 不使用傳感器 基本等同於unspecified
僅landscape和portrait經常使用,表明界面默認是橫屏或者豎屏,還能夠再代碼中更改。

 

--------------------- 

相關文章
相關標籤/搜索