Android中Fragment與Activity的生命周期對(duì)比
Fragment必須是依存于Activity而存在的,因此Activity的生命周期會(huì)直接影響到Fragment的生命周期。官網(wǎng)這張圖很好的說(shuō)明了倆者的關(guān)系:

可以看到Fragment比Activity多了幾個(gè)額外的生命周期回調(diào)函數(shù):
onAttach(Activity); //當(dāng)Activity與Fragment發(fā)生關(guān)聯(lián)時(shí)調(diào)用 onCreateView(LayoutInflater,ViewGroup,Bundle); //創(chuàng)建該Fragment的視圖 onActivityCreate(bundle); //當(dāng)Activity的onCreate();方法返回時(shí)調(diào)用 onDestoryView(); //與onCreateView相對(duì)應(yīng),當(dāng)改Fragment被移除時(shí)調(diào)用 onDetach(); //與onAttach()相對(duì)應(yīng),當(dāng)Fragment與Activity的關(guān)聯(lián)被取消時(shí)調(diào)用
注意:除了onCreateView,其他的所有方法如果你重寫了,必須調(diào)用父類對(duì)于該方法的實(shí)現(xiàn)。
場(chǎng)景演示 :
切換到該Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach 11-29 14:26:35.095: D/AppListFragment(7649): onCreate 11-29 14:26:35.095: D/AppListFragment(7649): onCreateView 11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated 11-29 14:26:35.120: D/AppListFragment(7649): onStart 11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕滅掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause 11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState 11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解鎖
11-29 14:33:13.240: D/AppListFragment(7649): onStart 11-29 14:33:13.275: D/AppListFragment(7649): onResume
切換到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause 11-29 14:33:33.655: D/AppListFragment(7649): onStop 11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切換回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView 11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated 11-29 14:33:55.825: D/AppListFragment(7649): onStart 11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause 11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState 11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到應(yīng)用
11-29 14:36:51.940: D/AppListFragment(7649): onStart 11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出應(yīng)用
11-29 14:37:03.020: D/AppListFragment(7649): onPause 11-29 14:37:03.155: D/AppListFragment(7649): onStop 11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView 11-29 14:37:03.165: D/AppListFragment(7649): onDestroy 11-29 14:37:03.165: D/AppListFragment(7649): onDetach
比Activity多了一些生命周期,完整和Activity對(duì)接上,大家好好利用。
相關(guān)文章
Android自定義View實(shí)現(xiàn)餅狀圖帶動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)餅狀圖帶動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
如何通過(guò)Android Stduio來(lái)編寫一個(gè)完整的天氣預(yù)報(bào)APP
這篇文章主要介紹了通過(guò)Android Stduio來(lái)編寫一個(gè)天氣預(yù)報(bào)APP,具體的實(shí)現(xiàn)是通過(guò)調(diào)用天氣預(yù)報(bào)接口來(lái)獲得天氣數(shù)據(jù),再將查詢的天氣信息存儲(chǔ)在SQLiteDatabase中,界面則用LIstView和GridView來(lái)搭建2021-08-08
android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮
這篇文章主要為大家介紹了android中g(shù)zip數(shù)據(jù)壓縮與網(wǎng)絡(luò)框架解壓縮實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android開發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效
這篇文章主要為大家介紹了Android開發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
怎樣實(shí)現(xiàn)android http-post方法實(shí)例說(shuō)明
android http-post方法在開發(fā)中如何實(shí)現(xiàn),具體代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06
Android Studio輕松構(gòu)建自定義模板的步驟記錄
這篇文章主要給大家介紹了關(guān)于Android Studio輕松構(gòu)建自定義模板的相關(guān)資料,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android開發(fā)使用Activity嵌套多個(gè)Fragment實(shí)現(xiàn)橫豎屏切換功能的方法
這篇文章主要介紹了Android開發(fā)使用Activity嵌套多個(gè)Fragment實(shí)現(xiàn)橫豎屏切換功能的方法,結(jié)合實(shí)例形式分析了Android使用Activity嵌套多個(gè)Fragment進(jìn)行橫豎屏切換的原理與具體操作技巧,需要的朋友可以參考下2017-11-11
Android自定義Drawable實(shí)現(xiàn)圓角效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Drawable實(shí)現(xiàn)圓角效果,實(shí)現(xiàn)一個(gè)圓形和圓角的背景圖片效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

