【Android基礎】Fragment 詳解之Fragment生命周期


上一篇文章簡單介紹了一下Fragment,這一篇文章會詳細的說一下Fragment的生命周期和創建一個用戶界面。

Fragment的主要功能就是創建一個View,並且有一個生命周期來管理這個View的創建和銷毀。Fragment的生命周期與Activity的生命周期類似,都會有一些回調方法,你所做的工作就是利用好這些生命周期方法,在恰當的方法中做恰當的工作。

Fragment的生命周期與Activity的狀態圖如下:

activity_fragment_lifecycle

左側是Activity的生命周期狀態,右側對應的是這個狀態下回執行Fragment的哪些生命周期方法。可以看到Fragment生命周期函數與Activity生命周期函數很多名字都是一樣的,對應的功能也類似,只不過在Created狀態和Destroyed狀態比Activity增加了一些方法。下面就說一下這幾個增加的生命周期方法:

  • onAttach():
    當fragment與它所在的Activity關聯起來的時候調用。
  • onCreatView():
    當需要創建一個與Fragment關聯的View時候會調用,這個方法會返回一個View,這個View會被添加到Activity的View樹中,如果你不想Fragment顯示一個View就返回null。
  • onDestroyView():
    當與Fragment關聯的那個View(在onCreatView()方法中創建的)與Fragment解除關聯,從View樹中移除的時候調用,在下次Fragment需要顯示一個View的時候會重新調用onCreatView方法。
  • onDetach():
    當Fragment與之前onAttach()是關聯起來的那個Activity解除關系的時候調用。

與Activity類似,Fragment可以停留(長時間存在)的三個狀態:

  • Resumed:
    Fragment的運行狀態,此時Fragment處於運行狀態,並且可以與用戶之間進行交互,類似Activity的Resumed狀態。
  • Paused:
    有其他Activity獲取焦點,前台運行,Fragment所在的Activity失去焦點,部分的顯示在前台Activity下面。
  • Stopped:
    Fragment不再可見,此時的情形可能是Fragment所在的Activity已經stopped了,或者fragment從Activity中移除到Fragment回退棧中。一個Stopped狀態的Fragment不沒有被銷毀,還在存活狀態,它的狀態和內部信息被系統記錄和保存,只是不可見,不可交互,此時很可能會被系統回收。

與Activity類似,你可以利用Bundle來記錄Fragment的狀態,當Activity被銷毀需要記錄Fragment狀態,並且在Activity重新創建的時候恢復Fragment的狀態。你可以保存Fragment的狀態在Fragment的onSaveInstanceState()回調方法中,在onCteat()、onCreatView()或者onActivityCreated()方法中進行恢復。

在生命周期中Activity與Fragment的最大不同之處是回退棧是相互獨立的,Activity的回退棧是系統來管理的,Fragment的回退棧是被宿主Activity來管理的,也就是說你可以來進行控制(調用addToBackStack()).

注意:在Fragment中你如果要獲取一個Context對象,你可以調用getActivity()方法,但是調用getActivity()方法必須要在Fragment於Activity關聯起來之后,否則getActivity()返回為null。

上面說的都是Fragment的一些生命周期相關知識,下面來看看如何創建Fragment並且添加一個View給Fragment。

要想給Fragment添加一個View,你就必須重寫onCreateView方法,在這個方法中創建一個View並且返回,這個View是Fragment的根View。在這個方法中系統給你提供了LayoutInflater對象,通過這個對象你可以從xml文件中創建一個View,代碼如下:

publicstaticclassExampleFragmentextendsFragment{ 
    @Override 
    publicView onCreateView(LayoutInflaterinflater,ViewGroupcontainer,
                             BundlesavedInstanceState){
        // Inflate the layout for this fragment 
        returninflater.inflate(R.layout.example_fragment,container,false);
    } 
}

在onCreatView方法中container參數是一個ViewGroup,這個ViewGroup是從Activity傳遞過來的,是Fragment的View將要嵌入的那個父View。這里需要注意一下LayoutInflater的inflate函數,在這里第三個參數是false。

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Parameters
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns

  • The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

在這解釋一下inflate的三個參數含義:

resource:是所要解析的layout文件ID;

root:一個ViewGroup,attachToRoot為true時會作為從xml文件解析出來的view的根View,如果attachToRoot為false時僅僅提供一些布局的參數給返回的rootView;

attachToRoot:決定解析的view是否會綁定到root參數提供的view上。

以上就是Fragment的生命周期和如何創建一個Fragment的View。

 

關注微信公眾平台:程序員互動聯盟(coder_online),你可以第一時間獲取原創技術文章,和(java/C/C++/Android/Windows/Linux)技術大牛做朋友,在線交流編程經驗,獲取編程基礎知識,解決編程問題。程序員互動聯盟,開發人員自己的家。

【答疑解惑】java中的全局變量


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM