Fragment在android3.0
(api11
)開始才有的,剛開始是為了適配平板而推出(現(xiàn)在用處很多),現(xiàn)在在Android日常開發(fā)中被越來越多的使用谆吴,F(xiàn)ragment不能獨(dú)立存在
它必須依附于四大組件之一Activity
(將此activity稱為宿主activity
)而存在,同時activity必須是FragmentActivity
及其子類(所以我們直接繼承AppCompatActivity (extends FragmentActivity
)同樣可以使用Fragment)
Fragment是Object類的子類
public class Fragment extends Object implements ComponentCallbacks2, View.OnCreateContextMenuListener
Fragment的子類有:
DialogFragment,ListFragment,PreferenceFragment,WebViewFragment使用Fragment需要使用FragmentManager來進(jìn)行管理Activity.getFragmentManager,Fragment.getFragmentManager
Lifecycle(生命周期)
由于它的使用依賴于宿主activity踩晶,所以fragment的生命周期與宿主Activity的生命周期息息相關(guān)-
當(dāng)宿主activity執(zhí)行到生命周期的onResume()時砰粹,F(xiàn)ragment的生命周期方法依次執(zhí)行以下方法來源于官網(wǎng)
- onAttach(Activity) called once the fragment is associated with its activity.
- onCreate(Bundle) called to do initial creation of the fragment.
- onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
-
onActivityCreated(Bundle) tells the fragment that its activity has completed its own
[Activity.onCreate()](https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle))
. - onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
- onStart() makes the fragment visible to the user (based on its containing activity being started).
- onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).
-
當(dāng)fragment不再被使用蛛淋,將執(zhí)行以下方法
- onPause()fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
- onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
- onDestroyView() allows the fragment to clean up resources associated with its View.
- onDestroy() called to do final cleanup of the fragment's state.
- onDetach() called immediately prior to the fragment no longer being associated with its activity.
啟動一個Activity時咙好,F(xiàn)ragment與Activity的生命周期分別執(zhí)行的方法如下
1、fragment的生命周期包含在整個宿主activity的方法中
2褐荷、fragment的創(chuàng)建在整個activity的創(chuàng)建完成之前勾效,fragment的銷毀在activity銷毀之前
3、相同生命周期方法叛甫,fragment先執(zhí)行與宿主activity
4葵第、無論宿主activity中有多少fragment,當(dāng)activity銷毀時合溺,所有的fragment均會銷毀
- Fragment的使用方式
getFragmentManager().beginTransaction().add(containerId, fragment實(shí)例對象).commit();
- 靜態(tài)加載
在activity的layout布局文件中使用卒密,fragment
標(biāo)簽并設(shè)置name
屬性,在啟動activity不需要添加額外代碼即可加載fragment
<fragment
android:name="FragmentTwo所在包.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
- 動態(tài)加載
在Activity的layout中不使用fragment標(biāo)簽
棠赛,使用一個容器(比如:FrameLayout
哮奇、RelativeLayout
等)替代
/**
* activity容器
*/
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
- 使用方法1:
fragmentManager為Activity的方法,F(xiàn)ragmentOne 繼承自app包下Fragment
fragmentManager.beginTransaction().replace(R.id.fragment_container, FragmentOne()).commit()
- 使用方法2:
(supportFragmentManager為FragmentActivity中方法睛约,F(xiàn)ragmentTwo為v4包下的framgnet)
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, FragmentTwo()).commit()
備注:動態(tài)添加多個fragment時鼎俘,每次都需要開啟一個新事務(wù)beginTransaction,因?yàn)槊看螆?zhí)行commit后均會置空辩涝,所以FragmentTransaction 不能定義為全局變量贸伐,否則程序crash
- Fragment的數(shù)據(jù)傳遞
//設(shè)置數(shù)據(jù)源
fragment.setArguments(Bundle bundle)
//獲取數(shù)據(jù)
Bundle bundle=getArguments();
-
Fragment
與Activity
的通信方式
fragment是依附于activity而存在的,所以能直接獲取彼此的實(shí)例對象(適用于activity中動態(tài)添加fragment的情況)-
fragment
中獲取activity的某個屬性或方法:在activity中將這個屬性或方法定義為public類型
怔揩,在fragment中通過((宿主Activity)getActivity).屬性(方法)
來獲取,注意進(jìn)行非空判斷捉邢,activity
中獲取fragment的屬性或方法通過fragment的實(shí)例對象來獲取即可(只需要在framgnet中將 對應(yīng)的聲明為public
類型即可) - 其他:通過廣播
BroadcastReceiver
,回調(diào)方法
,EventBus
等
-
Fragment與Fragment的通信方式
通過宿主activity來實(shí)現(xiàn)通信
,BroadcastReceiver
全局變量
等Back Stack
// on to the back stack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);//添加到回退棧
ft.commit();
- Fragment設(shè)置動畫