現在很多app應用都使用了Fragment,不僅僅是因爲Fragment使得UI設計更加靈活,還有在性能上有大幅度提高,並且佔用內存降低,在同樣的界面Activity佔用內存比Fragment要多,響應速度Fragment比Activty在中低端手機上快幾倍!
- 官方介紹:
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a “sub activity” that you can reuse in different activities).
翻譯: Fragment是Activity的一部分內容. 你可以在一個單獨的Activity中合併放置多個Fragment, 用來構建一個多面板的UI界面, 並且還可以複用Fragment在多個Activity. 可以把他想成是Activity的一個子模塊. 你可以在Activity運行時, 添加或移除Fragment.擁有自己的生命週期, 接收自己的事件.(類似於一個可以在不同Activity種複用的子Activity)。本篇文章我主要介紹以下幾個基礎知識點,下篇重點說明Fragment與Fragment、Activity通信:
- Fragment的創建和使用
- Fragment的生命週期
- Fragment常用方法 add(), show(), hide(), replace()
一.Fragment的創建和使用
靜態創建
注意:
1. 配置andriod:name指定在layout中實例化的Fragment類
2. 至少配置 id 或 tag, 否則會拋異常
1.在佈局文件中添加Fragment的佈局中配置fragment標籤
<fragment
android:id="@+id/fragment_first"
android:tag="first"
android:name="cn.huyaru.com.fragmentdemo.FirstFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
2.創建Fragment的佈局文件
3.創建Fragment, 重寫onCreateView, 配置真正內容
public class FirstFragment extends Fragment {
// 重寫 onCreateView 配置佈局內容
/**
* @param inflater 佈局填充器, 打氣筒
* @param container Fragment所在的佈局容器
* @param savedInstanceState 之前保存的狀態
* @return
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** * resource:Fragment需要加載的佈局文件
* root:加載layout的父ViewGroup
* attactToRoot:false,不返回父ViewGroup
* */
View rootView = inflater.inflate(R.layout.fragment_first , container , false);
return rootView;
}
}
動態創建
1. 在layout佈局中添加一個佈局容器
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
2.創建Fragment, 重寫onCreateView, 配置真正內容
3.通過Fragment管理器添加/替換Fragment
// 1. 獲取Fragment管理器.
FragmentManager fm = getFragmentManager();
// 2. 開啓Fragment處理事務.
FragmentTransaction ft = fm.beginTransaction();
// 3. 進行Fragment操作
ft.replace(R.id.container, new FirstFragment());
// 4. 進行事務的提交
ft.commit();
Fragment的向下兼容
- Fragment是在Andriod3.0纔出現的. API11
v4包提供了Fragment, 將Fragment兼容到3.0及以下版本.
不同的地方:
- Fragment繼承 android.support.v4.app.Fragment
- Activity繼承 android.support.v4.app.FragmentActivity
- getFragmentManager() -> getSupportFragmentManager() 獲取管理器
二 .Fragment的生命週期
Fragment總是必須被嵌入到一個activity中。它們的生命週期直接受其宿主activity的生命週期影響。
- onAttach()
當fragment被綁定到activity時被調用(Activity會被傳入)。 - onCreateView()
創建和fragment關聯的view hierarchy時被調用。 - onActivityCreated()
當activity的onCreate()方法返回時被調用。 - onDestroyView()
當和fragment關聯的view hierarchy被移除時調用。 - onDetach()
當fragment從activity解除關聯時被調用。
注意 - 通過佈局文件的fragment標籤關聯的Fragment的生命週期
特點:跟Activity的生命週期高度的吻合。 - 通過代碼動態添加的Fragment的生命週期
特點:當提交事務之後,纔會走生命週期方法。
activity和fragment的生命週期最顯著的差異是各自如何在它的後臺堆棧中儲存。默認情況下,activity在停止後,它會被放到一個由系統管理的用於保存activity的back stack(因此用戶可以使用BACK按鍵導航回退)。但是,當你在一個事務期間移除fragment時,只有顯式調用addToBackStack()請求保存實例,纔會被放到一個由宿主activity管理的back stack(會退棧)。
三. Fragment常用方法(add(), show(), hide(), replace())
- 通過add方法(效率高)
特點:第一次add的時候執行生命週期方法,之後的show和hide是不影響生命週期的 - 通過Replace方法(少,效率低)
特點:沒replace一次,就會把之前老的Fragment給銷燬掉,然後重新創建一個新的。
注意:如果你的app有大量圖片,這時更好的方式可能是replace,配合你的圖片框架在Fragment視圖銷燬時,回收其圖片所佔的內存。