Fragment官方解析
由于fragment和activity的生命周期很類似,對(duì)activity不熟悉的可以參考–深入了解Activity-生命周期,
深入理解Activity-任務(wù),回退棧,啟動(dòng)模式,
概要
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).
一個(gè)Fragment代表了一種行為或者在一個(gè)Activity里面的一部分用戶界面。你可以在一個(gè)Activity里面放置多個(gè)fragments來(lái)構(gòu)建多面板的界面,也可以在多個(gè)Activity重用一個(gè)Fragment,你可以把fragment想象成一個(gè)Activity模塊化的一部分。它有自己的生命周期,接受自己的輸入事件,你也可以在一個(gè)Activity運(yùn)行的時(shí)候動(dòng)態(tài)的去添加或者移除fragment(從某種程度上說(shuō),就像一個(gè)“子Activity”,你可以使用在不同的Activity中)。
A fragment must always be embedded in an activity and the fragment’s lifecycle is directly affected by the host activity’s lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that’s managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.
fragment總是被嵌套在activity中,它的生命周期受它所在的activity的生命周期影響。例如,當(dāng)activity是暫停狀態(tài)時(shí),在activity中的fragment也是暫停狀態(tài),當(dāng)activity被銷毀的時(shí)候,在activity中的fragment也就被銷毀。但是當(dāng)activity是運(yùn)行狀態(tài)的時(shí)候(即resumed),你可以動(dòng)態(tài)的去操作每個(gè)fragment,比如添加、移除。當(dāng)你執(zhí)行一個(gè)fragment事物的時(shí)候,在activity的回退棧里會(huì)維護(hù)所有發(fā)生的fragment事物。回退棧允許用戶在點(diǎn)擊回退鍵的時(shí)候回到上次保存的fragment。
When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity’s view hierarchy and the fragment defines its own view layout. You can insert a fragment into your activity layout by declaring the fragment in the activity’s layout file, as a element, or from your application code by adding it to an existing ViewGroup. However, a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity.
當(dāng)你添加一個(gè)fragment作為activity布局的一部分,它就存在于activity布局層次結(jié)構(gòu)的一個(gè)ViewGroup當(dāng)中,并且它可以定義它自己的布局。你可以通過(guò)在activity的布局文件當(dāng)中用 來(lái)把一個(gè)fragment加入到activity當(dāng)中,或者在代碼中添加到ViewGroup中。fragment也可以沒(méi)有用戶界面。
This document describes how to build your application to use fragments, including how fragments can maintain their state when added to the activity’s back stack, share events with the activity and other fragments in the activity, contribute to the activity’s action bar, and more.
該文檔表述了怎樣去用fragment來(lái)構(gòu)建你的應(yīng)用,包括了fragment在被添加到activity的回退棧之后怎么去維護(hù)它的狀態(tài),怎樣與activity、在activity中其他fragment共享事件。怎樣集成action bar等等。
Design Philosophy 設(shè)計(jì)原理
Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet’s screen is much larger than that of a handset, there’s more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity’s appearance at runtime and preserve those changes in a back stack that’s managed by the activity.
Android在3.0版本引入了fragment,主要是為了支持在大屏幕實(shí)現(xiàn)更動(dòng)態(tài)更靈活的界面設(shè)計(jì),比如平板。因?yàn)槠桨宓钠聊槐仁謾C(jī)大,它有更多的空間來(lái)組合UI控件。fragment就是為了滿足不需要去管理復(fù)雜的view層次結(jié)構(gòu)這樣的需求而設(shè)計(jì)的。通過(guò)在activity中放置多個(gè)fragment的,你就能夠在運(yùn)行時(shí)修改activity的樣子并且把這些改變保存在activity維持的回退棧中。
For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in the tablet layout in figure 1.
例如,一個(gè)新聞應(yīng)用可以使用一個(gè)fragment將一列文章展示在左邊,另一個(gè)fragment在右邊展示內(nèi)容–這樣兩個(gè)fragment都顯示在一個(gè)activity上,每個(gè)fragment有屬于自己的一系列生命周期的回調(diào)方法來(lái)處理自己的用戶事件。這樣就不用選擇了一個(gè)文章之后再進(jìn)入到另一個(gè)activity去閱讀文章,用戶可以在同一個(gè)activity中左邊選擇文章,右邊閱讀文章。
圖1說(shuō)明了這個(gè)例子。
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment. This is especially important because a modular fragment allows you to change your fragment combinations for different screen sizes. When designing your application to support both tablets and handsets, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset, it might be necessary to separate fragments to provide a single-pane UI when more than one cannot fit within the same activity.
你應(yīng)該把每個(gè)fragment當(dāng)作模塊化的、可重用的activity組件來(lái)使用。這就是說(shuō),由于每個(gè)fragment定義了它自己的布局和行為并且擁有自己的生命周期,你可以將一個(gè)fragment放在多個(gè)activitie中使用,因?yàn)槟銘?yīng)用重用來(lái)避免去使用一個(gè)又一個(gè)的fragment。這是非常重要的,因?yàn)槟K化的fragment允許你在不同尺寸的屏幕大小上組合這些fragment。如果你要做一個(gè)同時(shí)支持平板和手機(jī)的應(yīng)用的話,你可以根據(jù)不同的布局配置、可以用屏幕空間來(lái)重用你的fragment來(lái)優(yōu)化用戶體驗(yàn)。例如,在手機(jī)上可能必需把這些fragment分開(kāi)放置在單一的用戶界面。
Creating a Fragment 創(chuàng)建一個(gè)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的子類)。fragment里面的代碼和Activity的很類似。它也有和activity相似的回調(diào)方法,比如onCreate(), onStart(), onPause(), and onStop()。實(shí)際上,當(dāng)你在轉(zhuǎn)換一個(gè)已經(jīng)存在的Android應(yīng)用,你可能只需要把a(bǔ)ctivity的回調(diào)方法中代碼放入fragment的回調(diào)方法中。
Usually, you should implement at least the following lifecycle methods:
通常,你至少應(yīng)該實(shí)現(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.
系統(tǒng)在創(chuàng)建該fragment的時(shí)候掉用該方法。在這里你應(yīng)該初始化一些fragment必要的組件,比如一些當(dāng)fragment暫停或者停止的時(shí)候需要保留的組件。
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.
當(dāng)fragment即將第一次創(chuàng)建自己的用戶界面時(shí)會(huì)掉用該方法,你必需返回一個(gè)View對(duì)象來(lái)構(gòu)建fragment的界面。如果你返回null的話該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).
當(dāng)用戶即將離開(kāi)該fragment的時(shí)候該方法會(huì)被調(diào)用(即使它不總是以為著fragment要被銷毀了),在該方法中我們應(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.
大多數(shù)應(yīng)用應(yīng)該在每個(gè)fragment中實(shí)現(xiàn)這三個(gè)方法,但是還有其他幾個(gè)方法,你可以用來(lái)處理fragment生命周期多變的狀態(tài)。
Handling the Fragment Lifecycle 處理Fragment的生命周期
Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity. Like an activity, a fragment can exist in three states:
管理fragment的生命與管理activity的生命周期很類似。像activity一樣,fragment可以有三種狀態(tài):
Resumed
The fragment is visible in the running activity.
fragment在運(yùn)行的activity中處理可見(jiàn)狀態(tài),并能接受用戶點(diǎn)擊事件。
Paused
Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn’t cover the entire screen).
另外一個(gè)activity處于前臺(tái)并且獲取焦點(diǎn),但是fragment的宿主activity仍然處于課件狀態(tài)(前臺(tái)activity部分透明或則沒(méi)有完全覆蓋整個(gè)屏幕)
Stopped
The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.
fragment不在可見(jiàn)。它所依附的宿主activity已經(jīng)停止了,并且fragment也被從activity中移除并且被添加到了回退棧中。一個(gè)stopped狀態(tài)的fragment仍然存在(所有的狀態(tài)和成員信息都被系統(tǒng)維護(hù)著)。然而,對(duì)于用戶來(lái)說(shuō),是看不見(jiàn)它的并且如果activity被干掉的話,它也被干掉了。
Also like an activity, you can retain the state of a fragment using a Bundle, in case the activity’s process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment’s onSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated().
就像一個(gè)activity一樣,你也可以用Bundle來(lái)保存fragment的狀態(tài),假設(shè)activity的進(jìn)程被殺死并且你需要在activity被重新創(chuàng)建的時(shí)候還原fragment的狀態(tài)。你可以在fragment的onSaveInstanceState()方法中保存狀態(tài),然后在onCreate(),onCreateView(), or onActivityCreated()方法中還原這些狀態(tài)。
The most significant difference in lifecycle between an activity and a fragment is how one is stored in its respective back stack. An activity is placed into a back stack of activities that’s managed by the system when it’s stopped, by default (so that the user can navigate back to it with the Back button, as discussed in Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling addToBackStack() during a transaction that removes the fragment.
在activity和fragment最大的不同點(diǎn)是各自的回退棧。當(dāng)一個(gè)activity處于stopped狀態(tài)的時(shí)候,它被放入一個(gè)由系統(tǒng)去管理的activityie回退棧。當(dāng)點(diǎn)擊回退鍵的時(shí)候,就會(huì)返回上個(gè)activitity。然而,fragment是被放入在由它所依附的宿主activity管理的一個(gè)回退棧。而且只有當(dāng)你在一個(gè)事物里調(diào)用用addToBackStack()才會(huì)添加進(jìn)去.
Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.
小提示: 如果你在Fragment中需要一個(gè)Context對(duì)象,你可以掉用getActivity()。然后,只有fragment被附加在一個(gè)
activity才會(huì)正常返回,否則會(huì)返回null.
好了,今天的Fragment就到這里,下一篇會(huì)講解Fragment的其他知識(shí)。
轉(zhuǎn)載于:https://www.cnblogs.com/yangqiangyu/p/5143079.html
總結(jié)
以上是生活随笔為你收集整理的Fragment官方解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 随机分配座位,共50个学生,使学号相邻的
- 下一篇: MVCWebForm对照学习:传值方式