亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中Fragment與Activity的生命周期對比

 更新時間:2016年02月25日 11:54:30   作者:高杰才_Android  
這篇文章主要介紹了Android中Fragment與Activity的生命周期對比,Fragment是在Activity的基礎(chǔ)之上進(jìn)行設(shè)計的,比Activity多出幾個控制生命周期的回調(diào)函數(shù),需要的朋友可以參考下

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

2016225115117535.jpg (373×684)

可以看到Fragment比Activity多了幾個額外的生命周期回調(diào)函數(shù):

onAttach(Activity);  //當(dāng)Activity與Fragment發(fā)生關(guān)聯(lián)時調(diào)用

onCreateView(LayoutInflater,ViewGroup,Bundle);  //創(chuàng)建該Fragment的視圖

onActivityCreate(bundle);  //當(dāng)Activity的onCreate();方法返回時調(diào)用

onDestoryView();  //與onCreateView相對應(yīng),當(dāng)改Fragment被移除時調(diào)用

onDetach();  //與onAttach()相對應(yīng),當(dāng)Fragment與Activity的關(guān)聯(lián)被取消時調(diào)用

注意:除了onCreateView,其他的所有方法如果你重寫了,必須調(diào)用父類對于該方法的實現(xiàn)。

場景演示 :

切換到該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對接上,大家好好利用。

相關(guān)文章

最新評論