徹底了解Android開發(fā)中Fragment的知識

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)

    1. onAttach(Activity) called once the fragment is associated with its activity.
    2. onCreate(Bundle) called to do initial creation of the fragment.
    3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
    4. 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)).
    5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
    6. onStart() makes the fragment visible to the user (based on its containing activity being started).
    7. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).
  • 當(dāng)fragment不再被使用蛛淋,將執(zhí)行以下方法

    1. 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.
    2. 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.
    3. onDestroyView() allows the fragment to clean up resources associated with its View.
    4. onDestroy() called to do final cleanup of the fragment's state.
    5. 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均會銷毀

啟動/銷毀宿主Activity時Fragment的生命周期方法執(zhí)行順序(靜態(tài)加載方式)
  • 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();
  • FragmentActivity的通信方式
    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è)置動畫
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末商膊,一起剝皮案震驚了整個濱河市伏伐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌晕拆,老刑警劉巖藐翎,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異实幕,居然都是意外死亡吝镣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門昆庇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來末贾,“玉大人,你說我怎么就攤上這事凰锡∥粗郏” “怎么了圈暗?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵掂为,是天一觀的道長裕膀。 經(jīng)常有香客問我,道長勇哗,這世上最難降的妖魔是什么昼扛? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮欲诺,結(jié)果婚禮上抄谐,老公的妹妹穿的比我還像新娘。我一直安慰自己扰法,他們只是感情好蛹含,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著塞颁,像睡著了一般浦箱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上祠锣,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天酷窥,我揣著相機(jī)與錄音,去河邊找鬼伴网。 笑死蓬推,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的澡腾。 我是一名探鬼主播沸伏,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼动分!你這毒婦竟也來了馋评?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤刺啦,失蹤者是張志新(化名)和其女友劉穎留特,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玛瘸,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蜕青,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了糊渊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片右核。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖渺绒,靈堂內(nèi)的尸體忽然破棺而出贺喝,到底是詐尸還是另有隱情菱鸥,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布躏鱼,位于F島的核電站氮采,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏染苛。R本人自食惡果不足惜鹊漠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望茶行。 院中可真熱鬧躯概,春花似錦、人聲如沸畔师。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽看锉。三九已至姿锭,卻和暖如春灿椅,著一層夾襖步出監(jiān)牢的瞬間读整,已是汗流浹背烈评。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工镊逝, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留陆盘,地道東北人兄裂。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓痹届,卻偏偏與公主長得像幸撕,于是被迫代替她去往敵國和親蹬蚁。 傳聞我的和親對象是個殘疾皇子恃泪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • 從前總想對你說句好久不見叽粹, 現(xiàn)在卻奢望能在夢里見你一面览效。 或許是緣分吧, 我們偶遇很多次虫几, 可那卻是令我失望的寒暄...
    拙溺閱讀 216評論 0 0
  • 為了提升金昌旅游文化品位锤灿,對外宣傳西部花城旅游景區(qū)。經(jīng)市政府積極爭取辆脸,國際自行車環(huán)青海湖賽增加了金昌賽段但校。為確保大...
    格斗勢閱讀 326評論 0 2
  • Kmoon_7753閱讀 111評論 0 0