Fragment生命週期分析測試

2021-06-22 00:09:01 字數 4557 閱讀 9592

一:官方

二: 乙個acticity包含乙個fragment

啟動activity,在oncreate中add fragment

06-08 12:13:05.806: d/activity(7125): oncreate

06-08 12:13:05.806: d/activity(7125): add fragment

06-08 12:13:05.806: d/activity(7125): onstart

06-08 12:13:05.806: d/fragment(7125): onattach

06-08 12:13:05.806: d/fragment(7125): oncreate

06-08 12:13:05.806: d/fragment(7125): oncreateview

06-08 12:13:05.816: d/fragment(7125): onviewcreated

06-08 12:13:05.816: d/fragment(7125): onactivitycreated

06-08 12:13:05.816: d/fragment(7125): onviewstaterestored

06-08 12:13:05.816: d/fragment(7125): onstart

06-08 12:13:05.816: d/activity(7125): onresume

06-08 12:13:05.816: d/fragment(7125): onresume

06-08 12:13:05.896: d/activity(7125): oncreateoptionsmenu

06-08 12:13:31.654: d/activity(7125): onpause

06-08 12:13:31.654: d/fragment(7125): onpause

06-08 12:13:32.385: d/activity(7125): onstop

06-08 12:13:32.385: d/fragment(7125): onstop

06-08 12:13:32.385: d/activity(7125): ondestroy

06-08 12:13:32.385: d/fragment(7125): ondestroyview

06-08 12:13:32.385: d/fragment(7125): ondestroy

06-08 12:13:32.385: d/fragment(7125): ondetach

按下home鍵,然後再回到程式

06-08 12:14:47.915: d/activity(7125): onpause

06-08 12:14:47.915: d/fragment(7125): onpause

06-08 12:14:48.696: d/activity(7125): onstop

06-08 12:14:48.696: d/fragment(7125): onstop

06-08 12:14:53.001: d/activity(7125): onstart

06-08 12:14:53.001: d/fragment(7125): onstart

06-08 12:14:53.001: d/activity(7125): onresume

06-08 12:14:53.001: d/fragment(7125): onresume

三: 乙個activity包含多個fragment,通過replace切換

先銷毀第乙個

06-08 12:45:22.223: d/fragment1(9942): onpause

06-08 12:45:22.223: d/fragment1(9942): onstop

06-08 12:45:22.223: d/fragment1(9942): ondestroyview

06-08 12:45:22.223: d/fragment1(9942): ondestroy

06-08 12:45:22.223: d/fragment1(9942): ondetach

增加第二個

06-08 12:45:22.223: d/fragment2(9942): onattach

06-08 12:45:22.223: d/fragment2(9942): oncreate

06-08 12:45:22.223: d/fragment2(9942): oncreateview

06-08 12:45:22.223: d/fragment2(9942): onviewcreated

06-08 12:45:22.223: d/fragment2(9942): onactivitycreated

06-08 12:45:22.223: d/fragment2(9942): onviewstaterestored

06-08 12:45:22.223: d/fragment2(9942): onstart

06-08 12:45:22.223: d/fragment2(9942): onresume

三: 乙個activity包含多個fragment,通過hide,show切換

無log產生, 剛開始add進去後,這幾個fragment都是出於活動狀態。  可以通過hide,show調整是否可見

但是不會對生命週期有影響。

getactivity().getsupportfragmentmanager().begintransaction().hide(p1).commit();

getactivity().getsupportfragmentmanager().begintransaction().show(p2).commit();

四: 乙個activity包含多個fragment,通過fragmentpageradapter,fragmentstatepageradapter切換

根據解釋,fragmentpageradapter會一直將資料儲存在記憶體中。不可見時,儲存3個fragment

超過三個時,對多餘的fragment這樣處理。

06-08 20:37:52.636: d/fragment5(6195): onpause

06-08 20:37:52.636: d/fragment5(6195): onstop

06-08 20:37:52.636: d/fragment5(6195): ondestroyview

新新增進來的fragment這樣處理

06-08 20:37:52.626: d/fragment2(6195): oncreateview

06-08 20:37:52.636: d/fragment2(6195): onviewcreated

06-08 20:37:52.636: d/fragment2(6195): onactivitycreated

06-08 20:37:52.636: d/fragment2(6195): onviewstaterestored

06-08 20:37:52.636: d/fragment2(6195): onstart

06-08 20:37:52.636: d/fragment2(6195): onresume

fragmentstatepageradapter會儲存3個,會從transaction中移除。

移除時:

06-08 20:40:03.846: d/fragment1(6618): onpause

06-08 20:40:03.846: d/fragment1(6618): onstop

06-08 20:40:03.846: d/fragment1(6618): ondestroyview

06-08 20:40:03.846: d/fragment1(6618): ondestroy

06-08 20:40:03.846: d/fragment1(6618): ondetach

加入時:

06-08 20:40:35.039: d/fragment1(6618): onattach

06-08 20:40:35.039: d/fragment1(6618): oncreate

06-08 20:40:35.039: d/fragment1(6618): oncreateview

06-08 20:40:35.049: d/fragment1(6618): onviewcreated

06-08 20:40:35.049: d/fragment1(6618): onactivitycreated

06-08 20:40:35.049: d/fragment1(6618): onviewstaterestored

06-08 20:40:35.049: d/fragment1(6618): onstart

06-08 20:40:35.049: d/fragment1(6618): onresume

Fragment生命週期

onattach 當碎片依附活動的時候被呼叫 oncreate 活動被建立 oncreateview 為碎片建立view,載入布局時呼叫 onactivitycreated 確保view已經被建立完畢時呼叫 onstart 活動被開啟,由不可見變為可見 onresume 活動已經準備好和使用者進行互...

Fragment生命週期

與activity類似,碎片有自己的生命週期。當碎片被建立時,會經歷 onattach oncreate oncreateview onactivitycreated 當碎片變得可見時,會經歷 onstart onresume 當碎片進入後台模式時,會經歷 onpause onstop 當碎片被銷毀...

理解Fragment生命週期

主要看兩張圖,和跑 一,fragment的生命周 二,與activity生命週期的對比 場景演示 切換到該fragment 螢幕滅掉 螢幕解鎖 切換回本身的fragment 回到桌面 回到應用 退出應用 1.當乙個fragment被建立的時候,它會經歷以下狀態.onattach oncreate o...