Fragment官方解析
由于fragment和activity的生命周期很類似,對activity不熟悉的可以參考–深入了解Activity-生命周期,
深入理解Activity-任務,回退棧,啟動模式,
概要
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).
一個Fragment代表了一種行為或者在一個Activity里面的一部分用戶界面。你可以在一個Activity里面放置多個fragments來構建多面板的界面,也可以在多個Activity重用一個Fragment,你可以把fragment想象成一個Activity模塊化的一部分。它有自己的生命周期,接受自己的輸入事件,你也可以在一個Activity運行的時候動態的去添加或者移除fragment(從某種程度上說,就像一個“子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的生命周期影響。例如,當activity是暫停狀態時,在activity中的fragment也是暫停狀態,當activity被銷毀的時候,在activity中的fragment也就被銷毀。但是當activity是運行狀態的時候(即resumed),你可以動態的去操作每個fragment,比如添加、移除。當你執行一個fragment事物的時候,在activity的回退棧里會維護所有發生的fragment事物。回退棧允許用戶在點擊回退鍵的時候回到上次保存的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.
當你添加一個fragment作為activity布局的一部分,它就存在于activity布局層次結構的一個ViewGroup當中,并且它可以定義它自己的布局。你可以通過在activity的布局文件當中用 來把一個fragment加入到activity當中,或者在代碼中添加到ViewGroup中。fragment也可以沒有用戶界面。
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來構建你的應用,包括了fragment在被添加到activity的回退棧之后怎么去維護它的狀態,怎樣與activity、在activity中其他fragment共享事件。怎樣集成action bar等等。
Design Philosophy 設計原理
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,主要是為了支持在大屏幕實現更動態更靈活的界面設計,比如平板。因為平板的屏幕比手機大,它有更多的空間來組合UI控件。fragment就是為了滿足不需要去管理復雜的view層次結構這樣的需求而設計的。通過在activity中放置多個fragment的,你就能夠在運行時修改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.
例如,一個新聞應用可以使用一個fragment將一列文章展示在左邊,另一個fragment在右邊展示內容–這樣兩個fragment都顯示在一個activity上,每個fragment有屬于自己的一系列生命周期的回調方法來處理自己的用戶事件。這樣就不用選擇了一個文章之后再進入到另一個activity去閱讀文章,用戶可以在同一個activity中左邊選擇文章,右邊閱讀文章。
圖1說明了這個例子。
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.
你應該把每個fragment當作模塊化的、可重用的activity組件來使用。這就是說,由于每個fragment定義了它自己的布局和行為并且擁有自己的生命周期,你可以將一個fragment放在多個activitie中使用,因為你應用重用來避免去使用一個又一個的fragment。這是非常重要的,因為模塊化的fragment允許你在不同尺寸的屏幕大小上組合這些fragment。如果你要做一個同時支持平板和手機的應用的話,你可以根據不同的布局配置、可以用屏幕空間來重用你的fragment來優化用戶體驗。例如,在手機上可能必需把這些fragment分開放置在單一的用戶界面。
Creating a Fragment 創建一個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的子類)。fragment里面的代碼和Activity的很類似。它也有和activity相似的回調方法,比如onCreate(), onStart(), onPause(), and onStop()。實際上,當你在轉換一個已經存在的Android應用,你可能只需要把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即將第一次創建自己的用戶界面時會掉用該方法,你必需返回一個View對象來構建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).
當用戶即將離開該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.
大多數應用應該在每個fragment中實現這三個方法,但是還有其他幾個方法,你可以用來處理fragment生命周期多變的狀態。
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可以有三種狀態:
Resumed
The fragment is visible in the running activity.
fragment在運行的activity中處理可見狀態,并能接受用戶點擊事件。
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).
另外一個activity處于前臺并且獲取焦點,但是fragment的宿主activity仍然處于課件狀態(前臺activity部分透明或則沒有完全覆蓋整個屏幕)
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不在可見。它所依附的宿主activity已經停止了,并且fragment也被從activity中移除并且被添加到了回退棧中。一個stopped狀態的fragment仍然存在(所有的狀態和成員信息都被系統維護著)。然而,對于用戶來說,是看不見它的并且如果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().
就像一個activity一樣,你也可以用Bundle來保存fragment的狀態,假設activity的進程被殺死并且你需要在activity被重新創建的時候還原fragment的狀態。你可以在fragment的onSaveInstanceState()方法中保存狀態,然后在onCreate(),onCreateView(), or onActivityCreated()方法中還原這些狀態。
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最大的不同點是各自的回退棧。當一個activity處于stopped狀態的時候,它被放入一個由系統去管理的activityie回退棧。當點擊回退鍵的時候,就會返回上個activitity。然而,fragment是被放入在由它所依附的宿主activity管理的一個回退棧。而且只有當你在一個事物里調用用addToBackStack()才會添加進去.
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中需要一個Context對象,你可以掉用getActivity()。然后,只有fragment被附加在一個
activity才會正常返回,否則會返回null.
好了,今天的Fragment就到這里,下一篇會講解Fragment的其他知識。
轉載于:https://www.cnblogs.com/yangqiangyu/p/5143079.html
總結
以上是生活随笔為你收集整理的Fragment官方解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 随机分配座位,共50个学生,使学号相邻的
- 下一篇: MVCWebForm对照学习:传值方式