由于本人英文能力實在有限,不足之初敬請諒解
本博客只要沒有注明“轉”,那么均為原創,轉貼請注明鏈接
?
本系列并沒有對原文100%翻譯,也沒有100%的貼出原文
?
與Activity通信
盡管Fragment已經作為一個依賴Activity的object實現,并且可以在多個activitiy內部使用,一個已知的fragment實例是直接與包含它的activity綁定的。
特別的,這個fragment可以通過getActivity()訪問Activity實例,并且輕松的執行如在activity布局中查找view一類的任務
View listView = getActivity().findViewById(R.id.list);
同樣的,你的activity可以通過使用indFragmentById()或者findFragmentByTag()從FragmentManager獲得一個fragment引用從而調用fragment中的方法
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
?
建立activity的事件回調
In some cases, you might need a fragment to share events with the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary.
一些情況下,你也許需要一個fragment來與activity共享events。一個好的方式是在fragment內部定義一個回調接口并且要求宿主activity實現它。當activity通過這個接口收到一個回調時,如果需要的話他可以與其他在布局中的fragments分享信息。
For example, if a news application has two fragments in an activity—one to show a list of articles (fragment A) and another to display an article (fragment B)—then fragment A must tell the activity when a list item is selected so that it can tell fragment B to display the article. In this case, the OnArticleSelectedListener interface is declared inside fragment A:
例如:如果一個新聞應用在一個activity有兩個fragment,一個用來顯示文章列表(fragment A),另一個用來顯示一篇文章(fragment B),那么當列表的條目被選中的時候fragment A必須告訴activity,這樣才能通知fragment B來顯示此文章。在這個例子中,OnArticleSelectedListener接口在fragment A內部聲明
public static class FragmentA extends ListFragment { ... // Container Activity must implement this interface public interface OnArticleSelectedListener { public void onArticleSelected(Uri articleUri); } ... }
?
fragment的宿主activity實現OnArticleSelectedListener接口并且覆蓋onArticleSelected()用來通知fragment B來自fragment A的事件。
為了保證宿主activity實現這個接口,fragment A的onAttach()回調方法(當往activity添加activity時由系統調用)通過強制轉換傳入onAttach()方法的Activity,實例化一個OnArticleSelectedListener實例。
public static class FragmentA extends ListFragment { OnArticleSelectedListener mListener; ... @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnArticleSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); } } ... }
?
如果activity沒有實現這個接口,那么fragment會拋出ClassCastException異常。如果成功,mListener成員變量會持有一個實現了OnArticleSelectedListener接口的activity引用,那么fragment A可以通過activity中調用OnArticleSelectedListener接口定義好的方法來分享event。例如:如果fragment A是ListFragment的一個擴展,每次用戶點擊列表條目的時候,系統調用fragment的onListItemClick()方法,其中fragment調用onArticleSelected()來與activity分享event
public static class FragmentA extends ListFragment { OnArticleSelectedListener mListener; ... @Override public void onListItemClick(ListView l, View v, int position, long id) { // Append the clicked item's row ID with the content provider Uri Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id); // Send the event and Uri to the host activity mListener.onArticleSelected(noteUri); } ... }
?
向Action Bar添加條目
你的fragment可以貢獻menu條目到activity的Options Menu通過實現onCreateOptionsMenu()。為了讓這個方法可以接收到調用,無論怎樣,在onCreate()期間,你必須調用setHasOptionsMenu()來告訴fragment愿意為Options Menu添加條目(否則,fragment不會收到onCreateOptionsMenu()的調用)
Any items that you then add to the Options Menu from the fragment are appended to the existing menu items. The fragment also receives callbacks to onOptionsItemSelected() when a menu item is selected.
任何從fragment追加到已經存在的menu條目上的條目添加到Options Menu中。當一個menu條目被選中時fragment也會收到onOptionsItemSelected()的回調
你也可以在你的fragment布局中注冊一個view通過調用registerForContextMenu()用來提供一個context menu。 當用戶打開context menu時,fragment會收到一個onCreateContextMenu()調用。當用戶選中一個條目時,fragment會收到一個onContextItemSelected()的調用。
Note: Although your fragment receives an on-item-selected callback for each menu item it adds, the activity is first to receive the respective callback when the user selects a menu item. If the activity's implementation of the on-item-selected callback does not handle the selected item, then the event is passed to the fragment's callback. This is true for the Options Menu and context menus.
注意:盡管fragment的每一個添加的menu條目都會收到一個on-item-selected回調,但activity會首先獲得相應的回調。如果activity中on-item-selected回調的實現沒有處理選中的item,那么event會傳遞給fragment的回調。對于Options Menu和context menus都是如此。
?
處理Fragment聲明周期
管理fragment的聲明周期與管理activity的聲明周期很像。和activity一樣,fragment可以存在于3中狀態:
Resumed
fragment在運行中的activity可見
Paused
另一個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.
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().
fragment是不可見的。宿主activity被stopped,或者fragment被從activity中remove掉但是添加到了back stack中。一個stopped的fragment仍然處于活著的狀態(所有的狀態和成員信息被系統保存著)。不管怎樣,它對用戶來說不再可見,并且activity被killed的時候fragment也隨之被kill掉。和activity一樣,你可以用Bundle保存fragment的狀態,萬一activity的進程被kill掉并且當activity重新create的時候你需要恢復fragment的狀態。 你可以在fragment的onSaveInstanceState()調用期間保存狀態,在onCreate()、onCreateView()或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的聲明周期最重要的區別是如何保存到他們各自的back stack中。當activity stopped的時候,它放置到由系統管理的activities的back stack中,默認地(所以用戶可以通過back按鍵導航回去,如 Tasks and Back Stack 中討論的一樣)。僅當你明確的要求在移除fragment的事務期間,通過調用addToBackStack()保存這個fragment實例時,fragment才會被放置到一個有宿主activity管理的back stack中。
Otherwise, managing the fragment lifecycle is very similar to managing the activity lifecycle. So, the same practices for managing the activity lifecycle also apply to fragments. What you also need to understand, though, is how the life of the activity affects the life of the fragment.
其他方面,管理fragment的聲明周期與管理activity的聲明周期十分相似。所以與管理activity聲明周期相同的練習也適用于fragments。你需要理解的是:activity的生命如何影響到fragment的生命
?
?
Coordinating with the activity lifecycle
與activity生命周期的協調
The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment. For example, when the activity receives onPause(), each fragment in the activity receives onPause().
fragment宿主activity的聲明周期直接影響到fragment生命周期,以至于對每一個fragment,activity每一個生命周期的回調導致一個相似的回調。例如:當activity收到onPause()時,activity中的每一個fragment也會收到onPause()
Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:
Fragments有一些附加的生命周期回調 ,他們處理獨有的與activity的交互,為了執行如建立和銷毀fragment的UI一類的action。這些附件的回調方法有:
onAttach()
Called when the fragment has been associated with the activity (the Activity is passed in here).
onCreateView()
Called to create the view hierarchy associated with the fragment.
onActivityCreated()
Called when the activity's onCreate() method has returned.
onDestroyView()
Called when the view hierarchy associated with the fragment is being removed.
onDetach()
Called when the fragment is being disassociated from the activity.
?
you can see how each successive state of the activity determines which callback methods a fragment may receive. For example, when the activity has received its onCreate() callback, a fragment in the activity receives no more than the onActivityCreated() callback.
可以看到每一個activity依次的狀態如何決定fragment會收到哪一個函數的回調。
當activity收到他的onCreate()回調時,activity中的fragmet至多會收到onActivityCreated()的回調
Once the activity reaches the resumed state, you can freely add and remove fragments to the activity. Thus, only while the activity is in the resumed state can the lifecycle of a fragment change independently.
一旦activit到了resumed狀態,你可以隨意在activity中的添加和刪除fragment。 僅當這個activity是處于resumed狀態下fragment的生命周期才可以獨立的改變。
However, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity.
無論怎樣,當activity離開resumed狀態時,fragment又一次被activity擠入它的下一個生命周期。
?
?
?
原文地址如下,英文水平實在有限,希望拍磚同時能給予指正。
http://developer.android.com/guide/topics/fundamentals/fragments.html
?
?
轉貼請保留以下鏈接
本人blog地址
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
