Lifecycle

會打這個記錄,最主要就是要用在狀態的儲存和恢復,還有旋轉螢幕的功能該如何實作。參考下面文章:

ClassMethod
MainonCreate
MainFragmentonAttach
MainonAttachFragment
MainFragmentonCreate
MainFragmentonCreateView
MainFragmentonViewCreated
MainFragmentonActivityCreated
MainonStart
MainFragmentonStart
MainonPostCreate
MainonResume
MainFragmentonResume
MainonPostResume
MainonCreateOptionsMenu
MainonPrepareOptionsMenu
ClassMethod
MainFragmentonPause
MainonPause
MainonSaveInstanceState
MainFragmentonSaveInstanceState
MainFragmentonStop
MainonStop
ClassMethod
MainonRestart
MainonStart
MainFragmentonStart
MainonResume
MainFragmentonResume
MainonPostResume

無任何訊息

ClassMethod
MainonCreate
MainFragmentonViewStateRestored
MainFragmentonAttach
MainonAttachFragment
MainFragmentonCreate
MainFragmentonCreateView
MainFragmentonViewCreated
MainFragmentonActivityCreated
MainonStart
MainFragmentonStart
MainonPostCreate
MainonResume
MainFragmentonResume
MainonPostResume
MainonCreateOptionsMenu
MainonPrepareOptionsMenu

第2個 MainFragment.onViewStateRestored(),有 API Level 17 的限制。

這兩個動作的結果是一樣的。

ClassMethod
MainFragmentonPause
MainonPause
MainFragmentonStop
MainonStop
MainFragmentonDestroyView
MainFragmentonDestroy
MainFragmentonDetach
MainonDestroy

不管有沒有要回傳,都會是以下的結果。

ClassMethod
MainFragmentonPause
MainonPause
SubonCreate
SubFragmentonAttach
SubonAttachFragment
SubFragmentonCreate
SubFragmentonViewCreated
SubFragmentonActivityCreated
SubonStart
SubFragmentonStart
SubonPostCreate
SubonResume
SubFragmentonResume
SubonPostResume
SubonCreateOptionsMenu
SubonPrepareOptionsMenu
MainonSaveInstanceState
MainFragmentonSaveInstanceState
MainFragmentonStop
MainonStop

包括有回傳值的也是一樣的結果

ClassMethod
SubFragmentonPause
SubonPause
MainFragmentonActivityResult, requestCode: 10 , resultCode: 10
MainonRestart
MainonStart
MainFragmentonStart
MainonResume
MainFragmentonResume
MainonPostResume
SubFragmentonStop
SubonStop
SubFragmentonDestroyView
SubFragmentonDestroy
SubFragmentonDetach
SubonDestroy
ClassMethod
MainFragmentonPause
MainonPause
MainonSaveInstanceState
MainFragmentonSaveInstanceState
MainFragmentonStop
MainonStop
MainFragmentonDestroyView
MainFragmentonDestroy
MainFragmentonDetach
MainonDestroy
MainonCreate
MainFragmentonAttach
MainonAttachFragment
MainFragmentonCreate
MainFragmentonCreateView
MainFragmentonViewCreated
MainFragmentonActivityCreated
MainonStart
MainFragmentonStart
MainonRestoreInstanceState
MainonCreateOptionsMenu
MainonPrepareOptionsMenu
MainonPostCreate
MainonResume
MainFragmentonResume
MainonPostResume

AudroidManifest.xml 加入 android:configChanges 設定:

  • android:configChanges=“orientation”
  • android:configChanges=“orientation|screenSize” (API level 13以上)
  • android:configChanges=“orientation|keyboardHidden” (使用 AVD 模擬器測試)
ClassMethod
MainonConfigurationChanged
MainFragmentonConfigurationChanged

剛剛翻轉螢幕的動作全不見了,只剩下 onConfigurationChanged。