Fragment销毁时replace和add两个方法的区别

这个首先从一个bug说起,如图: 我们都知道fragment切换有两种方式: replace方式 transaction.replace(R.id.content, IndexFragment); add-hide-show方式 transaction.add(R.id.content, IndexFragment); transaction.hide(otherfragment); transaction.show(thisfragment); 而上面按钮中出现bug的就是采用第二种方式。然后我们来分析下用add,hide,show为什么出现这种bug,我把每个操作都打印出了以下日志: 复现bug的操作是: 1.首先打开,默认选中的是第一个tab,如上面的一张图片正常那样。 2.切换到tab2,并把tab1 hide掉; 3.再切回到tab1,并不会触发tab1对应fragment的任何生命周期; 4.然后home键进入后台,我在activity的onPause()中手动对IndexFragment赋空,模拟长时间后台,系统销毁了该引用。 IndexFragment=null; 5.再次启动,其实tab1 的fragment实例在内存中还在,只是他的引用被销毁了。 6.再切到tab2,这里其实是先把tab1的hide,在show tab2,但是tab1 的fragment引用为空,所以无法hide,就出现了tab2叠在tab1上的花屏情况。 7.再切到tab1,tab1就会重复创建对象。 同样的操作,我们使用replace的方式 使用replace方式,虽然这种方式会避免上述的bug,但也是重复创建了对象。因为replace方式,对应的FrameLayout只有一 层,而add方式,这个FrameLayout其实有2层。但是这种方式的缺点是:每次replace会把生命周期全部执行一遍,如果在这些生命周期函数 里拉取数据的话,就会不断重复的加载刷新数据。 那么最合适的处理方式是这样的: 1.在add的时候,加上一个tab参数 transaction.add(R.id.content, IndexFragment,”Tab1″); 2.然后当IndexFragment引用被回收置空的话,先通过 IndexFragment=FragmentManager.findFragmentByTag(“Tab1″); 找到对应的引用,然后继续上面的hide,show;

2016年7月27日 · 1 分钟 · 天边的星星

Android DrawerLayout+ fragment 布局实现左右侧滑

技术要点: android.support.v4.widget.DrawerLayout 打开抽屉: DrawerLayout .openDrawer(); 关闭抽屉:DrawerLayout.closeDrawer( ); **为slidingLayout设置一个layout_grative属性 ** ** ** **中间![](http://www.2cto.com/uploadfile/Collfiles/20140226/20140226092627200.jpg) 左侧![](http://www.2cto.com/uploadfile/Collfiles/20140226/20140226092627201.jpg) 右侧 ![](http://www.2cto.com/uploadfile/Collfiles/20140226/20140226092627202.jpg) ** 点击first ![](http://www.2cto.com/uploadfile/Collfiles/20140226/20140226092627203.jpg) 点击second ![](http://www.2cto.com/uploadfile/Collfiles/20140226/20140226092628204.jpg) ** ** ** ** 代码: activity_main.xml <喎�”http://www.2cto.com/kf/ware/vc/” target=”_blank” class=”keylink”>vc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz48L3N0cm9uZz48L3A+CjxwcmUgY2xhc3M9″brush:java;”><frameLayout android:id=”@+id/fragment_layout” android:layout_width=”fill_parent” android:layout_height=”fill_parent” > first.xml [?](http://www.2cto.com/kf/201402/281540.html#) <table style="font-weight: normal !important;" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="gutter" style="color: #afafaf !important;"> <div class="line number1 index0 alt2"> 1 </div> <div class="line number2 index1 alt1"> 2 </div> <div class="line number3 index2 alt2"> 3 </div> <div class="line number4 index3 alt1"> 4 </div> <div class="line number5 index4 alt2"> 5 </div> </td> <td class="code"> <div class="container"> <div class="line number1 index0 alt2"> `&lt;linearlayout xmlns:android=``"&lt;a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android&lt;/a>"` `android:id=``"@+id/drawer_layout"` `android:layout_width=``"match_parent"` `android:layout_height=``"match_parent"` `android:orientation=``"vertical"``&gt;` </div> <div class="line number2 index1 alt1"> </div> <div class="line number3 index2 alt2"> ` ``&lt;textview android:id=``"@+id/textView1"` `android:layout_width=``"wrap_content"` `android:layout_height=``"wrap_content"` `android:text=``"first"` `android:textappearance=``"?android:attr/textAppearanceLarge"``&gt;` </div> <div class="line number4 index3 alt1"> ` ` </div> <div class="line number5 index4 alt2"> `&lt;/textview&gt;&lt;/linearlayout&gt;` </div> </div> </td> </tr> </table> ** second.xml** ...

2014年8月5日 · 13 分钟 · 天边的星星