目錄
1.Fragment簡(jiǎn)述
2.Fragment使用
3.Fragment與Activity交互
4.Fragment與Activity的生命周期
Fragment簡(jiǎn)述
Fragment是在Android 3.0 (API level 11)開(kāi)始引入的,它能讓你的app在現(xiàn)有基礎(chǔ)上性能大幅度提高法焰,并且占用內(nèi)存降低憎茂,同樣的界面Activity占用內(nèi)存比Fragment要多产场,響應(yīng)速度Fragment比Activty在中低端手機(jī)上快了很多根穷,甚至能達(dá)到好幾倍莽鸭,"單Activity + 多Fragment架構(gòu)"和"多模塊Activity + 多Fragment架構(gòu)"應(yīng)運(yùn)而生
Fragment使用
1.編寫(xiě)Fragment布局
創(chuàng)建fragment01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment01"/>
</LinearLayout>
2.創(chuàng)建Fragment的子類(lèi)
通常, 應(yīng)當(dāng)至少實(shí)現(xiàn)如下的生命周期方法:
onCreate()
當(dāng)創(chuàng)建fragment時(shí), 系統(tǒng)調(diào)用該方法.
在實(shí)現(xiàn)代碼中,應(yīng)當(dāng)初始化想要在fragment中保持的必要組件, 當(dāng)fragment被暫途姆迹或者停止后可以恢復(fù).
onCreateView()
fragment第一次繪制它的用戶(hù)界面的時(shí)候, 系統(tǒng)會(huì)調(diào)用此方法. 為了繪制fragment的UI,此方法必須返回一個(gè)View, 這個(gè)view是你的fragment布局的根view. 如果fragment不提供UI, 可以返回null.
onPause()
用戶(hù)將要離開(kāi)fragment時(shí),系統(tǒng)調(diào)用這個(gè)方法作為第一個(gè)指示(然而它不總是意味著fragment將被銷(xiāo)毀.) 在當(dāng)前用戶(hù)會(huì)話結(jié)束之前,通常應(yīng)當(dāng)在這里提交任何應(yīng)該持久化的變化(因?yàn)橛脩?hù)有可能不會(huì)返回).
創(chuàng)建Fragment01沿后,因?yàn)槔又衒ragment01布局簡(jiǎn)單彪蓬,所以只實(shí)現(xiàn)了onCreateView()
public class Fragment01 extends Fragment {
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01,container,false);
return view;
}
}
3.將Fragment添加到Activity中
將Fragment添加到Activity中有兩種方式,一種是在在activity的layout文件中聲明fragment悲立;另一種是動(dòng)態(tài)添加鹿寨,下面講動(dòng)態(tài)添加方法。
動(dòng)態(tài)添加Fragment時(shí)薪夕,需要在Activity的layout文件中聲明一個(gè)ViewGroup脚草。當(dāng)activity運(yùn)行的任何時(shí)候, 都可以將fragment添加到ViewGroup。
Activity布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_fragment01"
android:text="fragment01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_fragment02"
android:text="fragment02"/>
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Activity邏輯如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
}
private void initView(){
Button bt_fragment01 = findViewById(R.id.bt_fragment01);
Button bt_fragment02 = findViewById(R.id.bt_fragment02);
bt_fragment01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment01 fragment01 = new Fragment01();
addFragment(fragment01);
}
});
bt_fragment02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment02 fragment02 = new Fragment02();
addFragment(fragment02);
}
});
}
private void addFragment(Fragment fragment){
//1.獲取到FragmentManager原献,在V4包中通過(guò)getSupportFragmentManager
//在系統(tǒng)中原生的Fragment是通過(guò)getFragmentManager獲得的馏慨。
android.support.v4.app.FragmentManager FM = getSupportFragmentManager();
//2.開(kāi)啟一個(gè)事務(wù),通過(guò)調(diào)用beginTransaction方法開(kāi)啟姑隅。
FragmentTransaction fragmentTransaction =FM.beginTransaction();
//3.向容器內(nèi)加入Fragment写隶,一般使用add或者replace方法實(shí)現(xiàn),需要傳入容器的id和Fragment的實(shí)例粤策。使用replace可以防止fragment重疊
fragmentTransaction.replace(R.id.fragment_container,fragment);
//4.提交事務(wù)樟澜,調(diào)用commit方法提交。
fragmentTransaction.commit();
}
}
Fragment動(dòng)態(tài)添加到Activity步驟:
1.獲取到FragmentManager
2.開(kāi)啟一個(gè)事務(wù)
3.向容器內(nèi)加入Fragment
4.提交事務(wù)
注意事項(xiàng):
在使用fragmentTransaction.add()向容器添加fragment時(shí),如果布局文件已經(jīng)添加了fragment秩贰,再次添加霹俺,會(huì)出現(xiàn)重疊現(xiàn)象,而使用replace不會(huì)毒费。
Fragment與Activity交互
盡管Fragment被實(shí)現(xiàn)為一個(gè)獨(dú)立于Activity的對(duì)象,并且可以在多個(gè)activity中使用,但一個(gè)給定的fragment實(shí)例是直接綁定到包含它的activity的. 特別的,fragment可以使用 getActivity() 訪問(wèn)Activity實(shí)例, 并且容易地執(zhí)行比如在activity layout中查找一個(gè)view的任務(wù).
View listView =getActivity().findViewById(R.id.list);
同樣地,activity可以通過(guò)從FragmentManager獲得一個(gè)到Fragment的引用來(lái)調(diào)用fragment中的方法, 使用findFragmentById() 或 findFragmentByTag().
ExampleFragment fragment (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment)
Fragment與Activity數(shù)據(jù)交互參考:
https://blog.csdn.net/chenliguan/article/details/53906934
http://www.reibang.com/p/1b824e26105b
Fragment與Activity的生命周期
fragment所生存的activity的生命周期,直接影響fragment的生命周期,每一個(gè)activity的生命周期的回調(diào)行為都會(huì)引起每一個(gè)fragment中類(lèi)似的回調(diào).
例如,當(dāng)activity接收到onPause()時(shí),activity中的每一個(gè)fragment都會(huì)接收到onPause().
Fragment 有一些額外的生命周期回調(diào)方法, 那些是處理與activity的唯一的交互,為了執(zhí)行例如創(chuàng)建和銷(xiāo)毀fragment的UI的動(dòng)作. 這些額外的回調(diào)方法是:
onAttach()
當(dāng)fragment被綁定到activity時(shí)被調(diào)用(Activity會(huì)被傳入.).
onCreateView()
創(chuàng)建和fragment關(guān)聯(lián)的view hierarchy時(shí)調(diào)用.
onActivityCreated()
當(dāng)activity的onCreate()方法返回時(shí)被調(diào)用.
onDestroyView()
當(dāng)和fragment關(guān)聯(lián)的view hierarchy正在被移除時(shí)調(diào)用.
onDetach()
當(dāng)fragment從activity解除關(guān)聯(lián)時(shí)被調(diào)用.