Fragment生命週期的奇怪設定
阿新 • • 發佈:2019-01-05
static final int INITIALIZING = 0; // Not yet created.
static final int CREATED = 1; // Created.
static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
static final int STOPPED = 3; // Fully created, not started.
static final int STARTED = 4 ; // Created and started, not resumed.
static final int RESUMED = 5; // Created started and resumed.
這些狀態的轉換過程中會呼叫Fragment的生命週期方法。如果在同一個狀態下呼叫的方法,不會因為Activity#finish的呼叫而導致後續方法不被呼叫。
神奇的是,狀態可能一次跳轉多次,所以很可能從起始態一路狂奔到中間態。比如從INITIALIZING一路幹到STARTED。
更神奇的是,銷燬狀態是靠回滾state完成的。
建立過程中狀態對應的方法:
- INITIALIZING:onAttach、onCreate、onCreateView、onViewCreated
- CREATED:onCreateView、onViewCreated、onActivityCreated
- ACTIVITY_CREATED、STOPPED:onStart
- STARTED:onResume
銷燬過程中狀態對應的方法:
- RESUMED:onPause
- STARTED:onStop
- ACTIVITY_CREATED:onDestroyView
- CREATED:onDestory、onDetach
總結起來,所有重寫的生命週期方法,一定會被調到,所有異常狀態都只能在最後一個重寫的方法中處理。