Fragment常用的三个类:
android.app.Fragment 主要用于定义Fragment
android.app.FragmentManager 主要用于在Activity中操作Fragment
android.app.FragmentTransaction 保证一些列Fragment操作的原子性
获取FragmentManage的方式:
getFragmentManager()
getSupportFragmentManager //v4中FragmentActivity
主要的操作都是FragmentTransaction的方法
FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add() //往Activity中添加一个Fragment
transaction.remove() //从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
transaction.replace()//使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide() //当你的fragment数量固定很少时隐藏当前的Fragment,仅仅是设为不可见,并不会销毁,多的时候可能出现OOM异常,
transaction.show()//显示之前隐藏的Fragment
detach()会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()重建view视图,附加到UI上并显示。
transatcion.commit()//提交一个事务
如果你喜欢使用Fragment,一定要清楚这些方法,哪个会销毁视图,哪个会销毁实例,哪个仅仅只是隐藏,这样才能更好的使用它们。
a、比如:我在FragmentA中的EditText填了一些数据,当切换到FragmentB时,如果希望会到A还能看到数据,则适合你的就是hide和show;
也就是说,希望保留用户操作的面板,你可以使用hide和show,当然了不要使劲在那new实例,进行下非null判断。
b、再比如:我不希望保留用户操作,你可以使用remove(),然后add();或者使用replace()这个和remove,add是相同的效果。
c、remove和detach有一点细微的区别,在不考虑回退栈的情况下,remove会销毁整个Fragment实例,而detach则只是销毁其视图结构,实例并不会被销毁。
那么二者怎么取舍使用呢?如果你的当前Activity一直存在,那么在不希望保留用户操作的时候,你可以优先使用detach。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/activity_main" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.example.lesson10_fragment2.MainActivity"> 8 9 <Button 10 android:id="@+id/btn_add" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:onClick="add" 14 android:text="添加Fragment" 15 android:textAllCaps="false" /> 16 17 <Button 18 android:id="@+id/btn_remove" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:onClick="remove" 22 android:text="移除Fragment" 23 android:textAllCaps="false" /> 24 25 <Button 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:onClick="replace" 29 android:text="替换Fragment" 30 android:textAllCaps="false" /> 31 32 <Button 33 android:id="@+id/btn_attach" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:onClick="attach" 37 android:text="附加Fragment" 38 android:textAllCaps="false" /> 39 40 <Button 41 android:id="@+id/btn_detach" 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:onClick="detach" 45 android:text="移除附加Fragment" 46 android:textAllCaps="false" /> 47 48 <Button 49 android:id="@+id/btn_show" 50 android:layout_width="match_parent" 51 android:layout_height="wrap_content" 52 android:onClick="show" 53 android:text="显示Fragment" 54 android:textAllCaps="false" /> 55 56 <Button 57 android:id="@+id/btn_hide" 58 android:layout_width="match_parent" 59 android:layout_height="wrap_content" 60 android:onClick="hide" 61 android:text="隐藏Fragment" 62 android:textAllCaps="false" /> 63 64 <!--动态加载,必须要给定一个空间用来加载Fragment 必须是一个FrameLayout !--> 65 <FrameLayout 66 android:id="@+id/fl_content" 67 android:layout_width="match_parent" 68 android:layout_height="wrap_content"> 69 70 </FrameLayout> 71 </LinearLayout>