Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 詳解Android Fragment之二:Fragment的創建和生命周期

詳解Android Fragment之二:Fragment的創建和生命周期

編輯:關於android開發

       上一節中講了Fragment概述及用法,本節繼續講解Fragment的創建和生命周期。

       Fragments的生命周期 

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

詳解Android Fragment之二:Fragment的創建和生命周期

       創建片元(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中保持的那些必要組件,當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時是很實用的。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved