1. 程式人生 > >Activity在旋轉螢幕時的生命週期

Activity在旋轉螢幕時的生命週期

模擬器版本為 android5.0 (以下資料測試)

主要根據在AndroidManifest中配置configChanges引數決定

1. 不進行任何配置和配置android:configChanges="orientation"引數:

  • 啟動 activity生命週期如下:
09-26 01:53:04.127 5376-5376/? D/TestApplication: onCreate...
09-26 01:53:04.135 5376-5376/? D/TestApplication: onStart...
    onResume...
  • 豎屏旋轉橫屏
09-26 01:57:06.180 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onPause...
09-26 01:57:06.181 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onStop...
    onDestroy...
09-26 01:57:06.222 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onCreate...
09-26 01:57:06.224 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onStart...
    onResume...
  • 橫屏旋轉為豎屏(和上面的一致)
09-26 01:57:33.069 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onPause...
09-26 01:57:33.070 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onStop...
    onDestroy...
09-26 01:57:33.103 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onCreate...
09-26 01:57:33.104 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onStart...
09-26 01:57:33.105 5376-5376/test.juyoufuli.com.testapplication D/TestApplication: onResume...

2. 配置 android:configChanges="orientation|screenSize>引數,監控螢幕旋轉

screenSize:裝置的螢幕的尺寸資訊發生了改變,旋轉螢幕時也會觸發這個,但是這個值是API13之後才有的,因此13之前的裝置沒有這個。

  • 只會呼叫onConfigurationChanged方法,不會呼叫activity生命週期方法
09-26 02:21:42.594 15709-15709/test.juyoufuli.com.testapplication D/TestApplication: onConfigurationChanged...
09-26 02:22:51.193 15709-15709/test.juyoufuli.com.testapplication D/TestApplication: onConfigurationChanged...

總結

  1. Activity 的生命週期變化也會影響到和它繫結的Fragment。
    所以要注意在發生旋轉後的資料儲存處理。