欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Fragment的生命周期詳解

 更新時間:2016年09月04日 14:50:00   投稿:lqh  
本文主要講解Android Fragments的生命周期,這里整理了相關(guān)資料及對Fragment 的創(chuàng)建,有需要的小伙伴看下

Fragments的生命周期

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

詳解Android Fragment之二:Fragment的創(chuàng)建和生命周期

        創(chuàng)建片元(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.

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

       Usually, you should implement at least the following lifecycle methods:

       一般情況下,你至少需要實現(xiàn)以下幾個生命周期方法:

       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.

       在創(chuàng)建fragment時系統(tǒng)會調(diào)用此方法。在實現(xiàn)代碼中,你可以初始化想要在fragment中保持的那些必要組件,當(dāng)fragment處于暫?;蛘咄V範顟B(tài)之后可重新啟用它們。

       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繪制用戶界面時系統(tǒng)會調(diào)用此方法。為fragment繪制用戶界面,這個函數(shù)必須要返回所繪出的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).

       系統(tǒng)回調(diào)用該函數(shù)作為用戶離開fragment的第一個預(yù)兆(盡管這并不總意味著fragment被銷毀)。在當(dāng)前用戶會話結(jié)束之前,通常要在這里提交任何應(yīng)該持久化的變化(因為用戶可能不再返回)。

       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.

       大部分應(yīng)用程序都應(yīng)該至少為每個fragment實現(xiàn)這三個方法,但是還有許多其他用以操縱fragment生命周期中各個階段的回調(diào)函數(shù)。所有生命周期中的回調(diào)函數(shù)在操縱fragment生命周期一節(jié)中稍后再做討論。

       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.

       顯示一個浮動的對話框。使用這個類創(chuàng)建對話框是使用Activity類對話框工具方法之外的另一個不錯的選擇,因為你可以把fragment對話框并入到由activity管理的fragments后臺棧中,允許用戶返回到一個已經(jīng)摒棄的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。并且提供了許多管理列表視圖的函數(shù),例如處理點擊事件的onListItemClick()回調(diào)函數(shù)。

       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對象的體系結(jié)構(gòu)列表,類似于preferenceActivity。這在為應(yīng)用程序創(chuàng)建“設(shè)置”activity時是很實用的。

       以上就是對Android Fragments

相關(guān)文章

最新評論