一、什么是Fragment?
Fragment:是Android3.0开始新增的概念,意为碎片。Fragment是依赖于Activity的,不能独立存在的。
二、为什么要有Fragment?
Android运行在各种各样的设备中,有小屏幕的手机,还有大屏幕的平板,电视等。同样的界面在手机上显示可能很好看,在大屏幕的平板上就未必了,手机的界面放在平板上可能会有过分被拉长、控件间距过大等情况。针对屏幕尺寸的差距,Fragment的出现能做到一个App可以同时适应手机和平板。这就是为什么要有Fragment的原因。
三、Fragment的特点
Fragment是一种可以嵌入在Activity当中的UI片段
用来组建Activity界面的局部模块, 也可以说一个Actiivty界面可以由多个Fragment组成
其行为与Activity很相似, 有自己对应的布局(包含具体的View), 它有自己的生命周期,接收自己的输入事件,并且可以从运行中的activity中添加或移除
一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity的影响
本质上会产生一个FrameLayout,它加载的布局为其子布局
优势:
-
模块化:我们不必把所有代码全部写在Activity中,而是把代码写在各自的Fragment中。
-
可重用:多个Activity可以重用一个Fragment。
-
可适配:根据硬件的屏幕尺寸、屏幕方向,能够方便地实现不同的布局,这样用户体验更好。
三、Fragment的生命周期
Fragment的生命周期和Activity类似,但比Activity的生命周期复杂一些,基本的生命周期方法如下图:
-
onAttach():Fragment和Activity相关联时调用。可以通过该方法获取Activity引用,还可以通过getArguments()获取参数。
-
onCreate():Fragment被创建时调用。
-
onCreateView():创建Fragment的布局。
-
onActivityCreated():当Activity完成onCreate()时调用。
-
onStart():当Fragment可见时调用。
-
onResume():当Fragment可见且可交互时调用。
-
onPause():当Fragment不可交互但可见时调用。
-
onStop():当Fragment不可见时调用。
-
onDestroyView():当Fragment的UI从视图结构中移除时调用。
-
onDestroy():销毁Fragment时调用。
-
onDetach():当Fragment和Activity解除关联时调用。
fragment生命周期解析:
当一个fragment被创建的时候,需调用以下生命周期方法:onAttach(), onCreate(), onCreateView(), onActivityCreated()
当这个fragment对用户可见的时候,需调用:onStart() ,onResume()
当这个fragment进入后台模式需调用:onPause(),onStop()
当这个fragment被销毁或者是持有它的Activity被销毁了,调用:onPause() ,onStop(), onDestroyView(), onDestroy() onDetach()
四、将fragment添加到Activity的两种方式
静态注册:以<fragment>标签的形式添加到Activity的布局当中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.wcystart.wcystart.FragmentActivity">
<fragment
android:id="@+id/first_fragment"
android:name="com.example.wcystart.wcystart.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<fragment
android:id="@+id/second_fragment"
android:name="com.example.wcystart.wcystart.SecondFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
动态注册:通过java代码将fragment添加到已存在的宿主Activity中
重点讲解的是动态添加、删除、替换fragment
动态添加fragment常用的类:
FragmentManager:用来管理Activity中的fragment,app包中使用getFragmentManager() v4包中getSupportFragmentManager
FragmentTransaction:事务,用来添加,移除,替换fragment,FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add() :往Activity中添加一个Fragment
transaction.remove() :从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈,这个Fragment实例将会被销毁。
transaction.replace():使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide():隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show():显示之前隐藏的Fragment
transaction.commit():提交一个事务
detach():会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
注意:在用fragment的时候,可能会经常遇到这样Activity状态不一致:State loss这样的错误。主要是因为:commit方法一定要在Activity.onSaveInstance()之前调用。
上述,基本是操作Fragment的所有的方式了,在一个事务开启到提交可以进行多个的添加、移除、替换等操作。
值得注意的是:如果你喜欢使用Fragment,一定要清楚这些方法,哪个会销毁视图,哪个会销毁实例,哪个仅仅只是隐藏,这样才能更好的使用它们。
attach():重建view视图,附加到UI上并显示。
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。
动态注册代码示例:
Activity布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_linera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.wcystart.wcystart.AddFragmentActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<RadioGroup
android:id="@+id/rg_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_home"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/home_button_selector"
android:text="首页" />
<RadioButton
android:id="@+id/rb_community"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/community_button_selector"
android:paddingTop="10dp"
android:text="发现" />
<RadioButton
android:id="@+id/rb_message"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/message_button_selector"
android:text="消息" />
<RadioButton
android:id="@+id/rb_me"
style="@style/MainButtonStyle"
android:drawableTop="@drawable/me_button_selector"
android:text="我的" />
</RadioGroup>
</LinearLayout>
MainButtonStyle:
<style name="MainButtonStyle">