Android Fragment的生命周期詳解
Fragments的生命周期
每一個(gè)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.
要?jiǎng)?chuàng)建一個(gè)fragment,必須創(chuàng)建一個(gè)fragment的子類(或是繼承自它的子類)。fragment類的代碼看起來很像activity。它與activity一樣都有回調(diào)函數(shù),例如onCreate(),onStart(),onPause(),和onStop()。事實(shí)上,如果你正在將一個(gè)現(xiàn)成的Android應(yīng)用轉(zhuǎn)而使用Fragment來實(shí)現(xiàn),可以簡單的將代碼從activity的回調(diào)函數(shù)移植到各自的fragment回調(diào)函數(shù)中。
Usually, you should implement at least the following lifecycle methods:
一般情況下,你至少需要實(shí)現(xiàn)以下幾個(gè)生命周期方法:
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時(shí)系統(tǒng)會(huì)調(diào)用此方法。在實(shí)現(xiàn)代碼中,你可以初始化想要在fragment中保持的那些必要組件,當(dāng)fragment處于暫?;蛘咄V?fàn)顟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繪制用戶界面時(shí)系統(tǒng)會(huì)調(diào)用此方法。為fragment繪制用戶界面,這個(gè)函數(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的第一個(gè)預(yù)兆(盡管這并不總意味著fragment被銷毀)。在當(dāng)前用戶會(huì)話結(jié)束之前,通常要在這里提交任何應(yīng)該持久化的變化(因?yàn)橛脩艨赡懿辉俜祷兀?/p>
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)該至少為每個(gè)fragment實(shí)現(xiàn)這三個(gè)方法,但是還有許多其他用以操縱fragment生命周期中各個(gè)階段的回調(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,這里還有幾個(gè)你可能會(huì)繼承的子類:
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.
顯示一個(gè)浮動(dòng)的對話框。使用這個(gè)類創(chuàng)建對話框是使用Activity類對話框工具方法之外的另一個(gè)不錯(cuò)的選擇,因?yàn)槟憧梢园裦ragment對話框并入到由activity管理的fragments后臺(tái)棧中,允許用戶返回到一個(gè)已經(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.
顯示一個(gè)由適配器管理的條目列表(例如SimpleCursorAdapter),類似于ListActivity。并且提供了許多管理列表視圖的函數(shù),例如處理點(diǎn)擊事件的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.
顯示一個(gè)Preference對象的體系結(jié)構(gòu)列表,類似于preferenceActivity。這在為應(yīng)用程序創(chuàng)建“設(shè)置”activity時(shí)是很實(shí)用的。
以上就是對Android Fragments
- Android實(shí)現(xiàn)Tab布局的4種方式(Fragment+TabPageIndicator+ViewPager)
- Android中Fragment的解析和使用詳解
- Android Fragment與Activity之間的相互通信實(shí)例代碼
- Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器
- Android開發(fā)技巧之Fragment的懶加載
- Android用Fragment創(chuàng)建選項(xiàng)卡
- Android Fragment+FragmentTabHost組件實(shí)現(xiàn)常見主頁面(仿微信新浪)
- Android Fragment多層嵌套重影問題的解決方法
- Android 中 Fragment 嵌套 Fragment使用存在的bug附完美解決方案
- Android中關(guān)于FragmentA嵌套FragmentB的問題
- Android Fragment(動(dòng)態(tài),靜態(tài))碎片詳解及總結(jié)
相關(guān)文章
Android Fragment的靜態(tài)注冊和動(dòng)態(tài)注冊創(chuàng)建步驟
這篇文章主要介紹了Android Fragment的靜態(tài)注冊和動(dòng)態(tài)注冊創(chuàng)建步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Flutter實(shí)戰(zhàn)教程之酷炫的開關(guān)動(dòng)畫效果
這篇文章主要給大家介紹了關(guān)于Flutter實(shí)戰(zhàn)教程之酷炫的開關(guān)動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11android10 隱藏SystemUI鎖屏下的多用戶圖標(biāo)的示例代碼
這篇文章主要介紹了android10 隱藏SystemUI鎖屏下的多用戶圖標(biāo),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Rocksdb?Memtable數(shù)據(jù)結(jié)構(gòu)源碼解析
這篇文章主要為大家介紹了Rocksdb?Memtable數(shù)據(jù)結(jié)構(gòu)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android實(shí)現(xiàn)圖片點(diǎn)擊爆炸效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片點(diǎn)擊爆炸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Android開發(fā)藝術(shù)探索學(xué)習(xí)筆記(七)
這篇文章主要介紹了Android開發(fā)藝術(shù)探索學(xué)習(xí)筆記(七)的相關(guān)資料,需要的朋友可以參考下2016-01-01android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法
本篇文章主要介紹了android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法,詳解的介紹了幾種實(shí)現(xiàn)咻一咻的思路和方法,有需要的可以了解一下。2016-11-11