Android Fragment詳解(二):fragment創建及其生命週期

Fragments的生命週期 

    每一個fragments 都有自己的一套生命週期回調方法和處理自己的用戶輸入事件。 對應生命週期可參考下圖:



創建片元(Creating a Fragment)

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.

要創建一個fragment,必須創建一個fragment的子類(或是繼承自它的子類)。fragment類的代碼看起來很像activity。它與activity一樣都有回調函數,例如onCreate(),onStart(),onPause(),和onStop()。事實上,如果你正在將一個現成的Android應用轉而使用Fragment來實現,可以簡單的將代碼從activity的回調函數移植到各自的fragment回調函數中。

Usually, you should implement at least the following lifecycle methods:
一般情況下,你至少需要實現以下幾個生命週期方法:
onCreate() 
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
在創建fragment時系統會調用此方法。在實現代碼中,你可以初始化想要在fragment中保持的那些必要組件(這裏的組件是指除了view之外的東西,比如需要進行界面展示的關鍵數據),當fragment處於暫停或者停止狀態之後可重新啓用它們。

onCreateView() 
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. 
在第一次爲fragment繪製用戶界面時系統會調用此方法。爲fragment繪製用戶界面,這個函數必須要返回所繪出的fragment的根View。如果fragment沒有用戶界面可以返回空。

onPause() 
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). 
系統回調用該函數作爲用戶離開fragment的第一個預兆(儘管這並不總意味着fragment被銷燬)。在當前用戶會話結束之前,通常要在這裏提交任何應該持久化的變化(因爲用戶可能不再返回)。

Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
大部分應用程序都應該至少爲每個fragment實現這三個方法,但是還有許多其他用以操縱fragment生命週期中各個階段的回調函數。所有生命週期中的回調函數在操縱fragment生命週期一節中稍後再做討論。

There are also a few subclasses that you might want to extend, instead of the base Fragment class:
除了基類fragment,這裏還有幾個你可能會繼承的子類:

DialogFragment 
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment. 
顯示一個浮動的對話框。使用這個類創建對話框是使用Activity類對話框工具方法之外的另一個不錯的選擇,因爲你可以把fragment對話框併入到由activity管理的fragments後臺棧中,允許用戶返回到一個已經摒棄的fragment。

ListFragment 
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events.
顯示一個由適配器管理的條目列表(例如SimpleCursorAdapter),類似於ListActivity。並且提供了許多管理列表視圖的函數,例如處理點擊事件的onListItemClick()回調函數。

PreferenceFragment 
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a "settings" activity for your application.
顯示一個Preference對象的體系結構列表,類似於preferenceActivity。這在爲應用程序創建“設置”activity時是很實用的。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章