Fragment生命周期分析测试

一:官方图片

Fragment:Activity:

                                      

     

二: 一个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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值