android橫豎屏切換時候Activity的生命週期_Android

來源:互聯網
上載者:User
1、建立一個Activity,並把各個生命週期列印出來

2、運行Activity,得到如下資訊

onCreate-->
onStart-->
onResume-->

3、按crtl+f12切換成橫屏時

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

4、再按crtl+f12切換成豎屏時,發現列印了兩次相同的log

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

5、修改AndroidManifest.xml,把該Activity添加

android:configChanges="orientation",執行步驟3
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->

6、再執行步驟4,發現不會再列印相同資訊,但多列印了一行onConfigChanged

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

7、把步驟5的android:configChanges="orientation" 改成
android:configChanges="orientation|keyboardHidden",執行步驟3,就只列印
onConfigChanged
onConfigurationChanged-->

8、執行步驟4

onConfigurationChanged-->
onConfigurationChanged-->

總結:

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

總結一下整個Activity的生命週期
補充一點,當前Activity產生事件彈出Toast和AlertDialog的時候Activity的生命
周期不會有改變
Activity運行時按下HOME鍵(跟被完全覆蓋是一樣的):onSaveInstanceState -->
onPause --> onStop onRestart -->onStart--->onResume
Activity未被完全覆蓋只是失去焦點:onPause--->onResume

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.