引言
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).
一個碎片代表在時間中的一種行為或者一部分的用戶交互挂捻,你可以在一個單一的活動(activity)中,組合復(fù)雜的碎片形成一個復(fù)合UI,并可以重復(fù)使用碎片在多個活動中.你可以將碎片想象成活動的一個組件,它有自己的生命周期,獲得到它自己的輸入事件碉纺,并且你可以添加或者移除碎片當(dāng)活動正在運行(有些類似活動的下級,可以在不同的活動中進行重復(fù)使用).
生命周期
fragment有它自己的生命周期刻撒,所以我們先來分析下生命周期.
onAttach(Activity)
called once the fragment is associated with its activity.
當(dāng)fragment和activity建立關(guān)系的時候調(diào)用onCreate(Bundle)
called to do initial creation of the fragment.
調(diào)用初始化創(chuàng)建fragmentonCreateView(LayoutInflater, ViewGroup, Bundle)
creates and returns the view hierarchy associated with the fragment.
創(chuàng)建和返回與fragment相關(guān)的布局骨田。onActivityCreated(Bundle)
tells the fragment that its activity has completed its own Activity.onCreate().
告知fragment activity已經(jīng)完成它的生命周期中onCreate這一步。onViewStateRestored(Bundle)
tells the fragment that all of the saved state of its view hierarchy has been restored.
告知fragment 布局的所有存儲狀態(tài)都已經(jīng)恢復(fù)声怔。即這里就是取出存儲到Bundle中的數(shù)據(jù).onStart()
makes the fragment visible to the user (based on its containing activity being started).
使得fragment對于用戶可見(這是基于包含碎片的activity可見的情況下).在這種情況下用戶只能看到視圖态贤,但是不能進行交互。onResume()
makes the fragment begin interacting with the user (based on its containing activity being resumed).
使得碎片開始和用戶進行交互(基于包含碎片的activity也開始交互的情況下)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.
碎片不再和用戶交互醋火,可能是activity進入paused狀態(tài)(activity進入半透明悠汽,半遮蓋的狀態(tài)),或者碎片被移除或者替代芥驳。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.
碎片不再對用戶可見柿冲,因為activity進stopped狀態(tài)(activity對用戶也不可見),或者fragment被移除在當(dāng)前activity中兆旬。onDestroyView()
allows the fragment to clean up resources associated with its View.
fragment清除和視圖有關(guān)的資源姻采。onDestroy()
called to do final cleanup of the fragment's state.
對碎片狀態(tài)的最后清理。onDetach()
called immediately prior to the fragment no longer being associated with its activity.
通知fragment和當(dāng)前activity解除關(guān)系爵憎。-
大致分為以下過程:
- 創(chuàng)建過程:onAttach->onCreate->onCreateView->onActivityCreated
- 可見化過程:onStart->onResume
- 后臺過程:onPause->onStop
- 銷毀過程:onDestoryView->onDestory->onDetach
按照生命周期的順序執(zhí)行慨亲。
注意:如上圖生命周期圖,用戶將程序放在后臺運行或者進入堆棧宝鼓,切換到其他視圖刑棵,就會執(zhí)行onPause->onStop.這兩個操作就是針對可視化到不可視化做出的響應(yīng)。
Back Stack
關(guān)于fragment的回退椨拚。可以看我的這篇文章.
狀態(tài)的恢復(fù)和存儲
方法
- onSaveInstanceState(Bundle outState)
這是用來儲存狀態(tài)的蛉签,比如成員變量等等胡陪,但是不需要手動存儲視圖數(shù)據(jù),因為android機制中View自動存儲恢復(fù)碍舍,如果你只想恢復(fù)視圖只需要實現(xiàn)這個方法柠座。
- onActivityCreated(Bundle)
首先我們要知道fragment中沒有和activity 一樣的onRestoreInstanceState方法進行狀態(tài)恢復(fù),所以我們一般在onActivityCreated進行狀態(tài)恢復(fù),當(dāng)然onCreate()片橡,onCreateView都可以妈经。
onSaveInstanceState調(diào)用:
This corresponds to Activity.onSaveInstanceState(Bundle) and most of the discussion there applies here as well. Note however: this method may be called at any time before onDestroy(). There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state.
和activity的onSaveInstanceState(Bundle)相當(dāng)。這個方法可能被調(diào)用在onDestory()之前的許多時機捧书,例如進入后臺吹泡,但是只有當(dāng)activity真的需要保存狀態(tài)的時候才會調(diào)用。
-
也就是說當(dāng)進入后臺過程(onPause->onStop->onDestoryView),都有可能調(diào)用onSaveInstanceState保存狀態(tài)经瓷,但是只有真的需要保存狀態(tài)的時候才會調(diào)用爆哑。我們來列舉下不需要調(diào)用的情況:
- fragmentB返回A,此時不會調(diào)用B的onSaveInstanceState方法舆吮,因為B不需要被恢復(fù)揭朝。
- 當(dāng)B在A之后被創(chuàng)建,如果在B的生存期間A并沒有被銷毀的話色冀,那么系統(tǒng)可能并不會調(diào)用A的onSaveInstanceState方法潭袱,因為這個時候的A仍然是完整的。
當(dāng)然在旋轉(zhuǎn)和跳轉(zhuǎn)的時候都會進行調(diào)用呐伞。
注意點:onSaveInstanceState中你只需要保存一些變量敌卓,即狀態(tài)的保存慎式。不需要存儲視圖資源伶氢,因為android View中已經(jīng)自動實現(xiàn)了存儲恢復(fù)機制,你只需要聲明了onSaveInstanceState方法瘪吏,View會自動在需要的時候進行存儲和恢復(fù)癣防。
getChildFragmentManager
- fragment中用getChildFragmentManager來管理fragment所嵌套的fragment。
- 所以對fragment進行管理有兩種掌眠,一種是在activity中對fragment進行管理蕾盯,一種是在fragment中管理嵌套的fragment.
總結(jié)
- fragment幾個重要概念:生命周期,堆棧管理蓝丙,存儲恢復(fù)级遭。
- 多實踐,理解會更加深刻渺尘。