android中fragment在后台回收时的生命周期

页面如下:activity可以切换fragment1,fragment2,初始状态是fragment2,切fragment1的时候回给fragment1传值,在实际测试中,发现app切后台再进来时,传的这个数据会报空。

先看声明周期,开始的时候的:

03-15 10:20:19.378 14280-14280/com.dxxx.testfragmentcarsh E/activity: onCreate
03-15 10:20:19.414 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onAttach
03-15 10:20:19.414 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onCreate
03-15 10:20:19.420 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onCreateView
03-15 10:20:19.423 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onActivityCreated
03-15 10:20:19.423 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onViewStateRestored
03-15 10:20:19.424 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onStart
03-15 10:20:19.424 14280-14280/com.dxxx.testfragmentcarsh E/activity: onStart
03-15 10:20:19.424 14280-14280/com.dxxx.testfragmentcarsh E/activity: onResume
03-15 10:20:19.424 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onResume

很正常,跟想象的一样,再切换到fragment1看看:

03-15 10:20:44.103 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onAttach
03-15 10:20:44.103 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onCreate
03-15 10:20:44.104 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onPause
03-15 10:20:44.104 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onDestroyView
03-15 10:20:44.105 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onDestroy
03-15 10:20:44.105 14280-14280/com.dxxx.testfragmentcarsh E/Fragment2: onDetach
03-15 10:20:44.107 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onCreateView  地址是:Fragment1{f1254d9 #1 id=0x7f0b0061}
03-15 10:20:44.117 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onActivityCreated
03-15 10:20:44.117 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onViewStateRestored
03-15 10:20:44.117 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onStart
03-15 10:20:44.117 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onResume

还是很正常,再后台关闭下再看:

03-15 10:21:22.211 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onPause
03-15 10:21:22.212 14280-14280/com.dxxx.testfragmentcarsh E/activity: onPause
03-15 10:21:22.277 14280-14280/com.dxxx.testfragmentcarsh E/Fragment1: onSaveInstanceState
03-15 10:21:22.278 14280-14280/com.dxxx.testfragmentcarsh E/activity: onStop

再从前台回来下:

03-15 10:22:29.515 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onAttach
03-15 10:22:29.515 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onCreate
03-15 10:22:29.781 16099-16099/com.dxxx.testfragmentcarsh E/activity: onCreate
03-15 10:22:29.796 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onCreateView  地址是:Fragment1{30bef2a1 #1 id=0x7f0b0061}
03-15 10:22:29.799 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onActivityCreated
03-15 10:22:29.799 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onViewStateRestored
03-15 10:22:29.806 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onAttach
03-15 10:22:29.806 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onCreate
03-15 10:22:29.811 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onCreateView
03-15 10:22:29.813 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onActivityCreated
03-15 10:22:29.813 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onViewStateRestored
03-15 10:22:29.813 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onStart
03-15 10:22:29.813 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onStart
03-15 10:22:29.813 16099-16099/com.dxxx.testfragmentcarsh E/activity: onStart
03-15 10:22:29.818 16099-16099/com.dxxx.testfragmentcarsh E/activity: onResume
03-15 10:22:29.818 16099-16099/com.dxxx.testfragmentcarsh E/Fragment1: onResume
03-15 10:22:29.818 16099-16099/com.dxxx.testfragmentcarsh E/Fragment2: onResume

会看到fragment1和fragment2都被重新实例化了,页面中也能看到fragment1和fragment2.注意:这里没有做传值的处理,否则就会发现fragment的那个传的参数空指针了!

这里因为activity被回收了,他在onsaveinstanc中保存了页面的数据,然后再次到前台会进行恢复,activity恢复会走activity的oncraet方法,也就是把fragment2显示出来,但是他在被销毁前保存了fragmen1的状态,所有也去走了fragemnt1的声明周期,但是传的那个数据,已经不在了,就报空了。

解决办法就是重写activity的 onSavaInstance方法,直接写空,那么状态就不会被保存了。

你可能感兴趣的:(Android)