Android Fragment 基本介紹及其生命週期講解

轉自:http://www.cnblogs.com/mengdd/archive/2013/01/08/2851368.html
http://blog.csdn.net/forever_crying/article/details/8238863/

Fragment

  Android是在Android 3.0 (API level 11)開始引入Fragment的。

  可以把Fragment想成Activity中的模塊,這個模塊有自己的佈局,有自己的生命週期,單獨處理自己的輸入,在Activity運行的時候可以加載或者移除Fragment模塊。

  可以把Fragment設計成可以在多個Activity中複用的模塊。

  當開發的應用程序同時適用於平板電腦和手機時,可以利用Fragment實現靈活的佈局,改善用戶體驗。

  如圖:

 

 

 

Fragment的生命週期

  因爲Fragment必須嵌入在Acitivity中使用,所以Fragment的生命週期和它所在的Activity是密切相關的。

  如果Activity是暫停狀態,其中所有的Fragment都是暫停狀態;如果Activity是stopped狀態,這個Activity中所有的Fragment都不能被啓動;如果Activity被銷燬,那麼它其中的所有Fragment都會被銷燬。

  但是,當Activity在活動狀態,可以獨立控制Fragment的狀態,比如加上或者移除Fragment。

  當這樣進行fragment transaction(轉換)的時候,可以把fragment放入Activity的back stack中,這樣用戶就可以進行返回操作。

 

 

 

Fragment的使用相關

  使用Fragment時,需要繼承Fragment或者Fragment的子類(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),所以Fragment的代碼看起來和Activity的類似。

 

使用Support Library

  Support Library是一個提供了API庫函數的JAR文件,這樣就可以在舊版本的Android上使用一些新版本的APIs。

  比如android-support-v4.jar.它的完整路徑是:

  <sdk>/extras/android/support/v4/android-support-v4.jar.

  它就提供了Fragment的APIs,使得在Android 1.6 (API level 4)以上的系統都可以使用Fragment。

  爲了確定沒有在舊版本系統上使用新版本的APIs,需要如下導入語句:

 

  import android.support.v4.app.Fragment;

  import android.support.v4.app.FragmentManager;

 

  同時應該將上述的包拷入libs項目下的libs文件夾,然後在項目的Properties中添加:右鍵單擊項目,選Properties,左邊選Java Build Path,然後Add External JARs…,添加android-support-v4.jar.

 

 

 

 

  當創建包含Fragment的Activity時,如果用的是Support Library,那麼繼承的就應該是FragmentActivity而不是Activity。

 

必須實現的三個回調函數

  onCreate()

  系統在創建Fragment的時候調用這個方法,這裏應該初始化相關的組件,一些即便是被暫停或者被停止時依然需要保留的東西。

  onCreateView()

  當第一次繪製Fragment的UI時系統調用這個方法,必須返回一個View,如果Fragment不提供UI也可以返回null。

  注意,如果繼承自ListFragment,onCreateView()默認的實現會返回一個ListView,所以不用自己實現。

  onPause()

  當用戶離開Fragment時第一個調用這個方法,需要提交一些變化,因爲用戶很可能不再返回來。

 

實現Fragment的UI

  提供Fragment的UI,必須實現onCreateView()方法。

  假設Fragment的佈局設置寫在example_fragment.xml資源文件中,那麼onCreateView()方法可以如下寫:

  

複製代碼
public static class ExampleFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}
複製代碼

 

  onCreateView()中container參數代表該Fragment在Activity中的父控件;savedInstanceState提供了上一個實例的數據。

  inflate()方法的三個參數:

  第一個是resource ID,指明瞭當前的Fragment對應的資源文件;

  第二個參數是父容器控件;

  第三個布爾值參數表明是否連接該佈局和其父容器控件,在這裏的情況設置爲false,因爲系統已經插入了這個佈局到父控件,設置爲true將會產生多餘的一個View Group。

 

把Fragment加入Activity

  當Fragment被加入Activity中時,它會處在對應的View Group中。

  Fragment有兩種加載方式:一種是在Activity的layout中使用標籤<fragment>聲明;另一種方法是在代碼中把它加入到一個指定的ViewGroup中。

  另外,Fragment它可以並不是Activity佈局中的任何一部分,它可以是一個不可見的部分。這部分內容先略過。

 

加載方式1:通過Activity的佈局文件將Fragment加入Activity

  在Activity的佈局文件中,將Fragment作爲一個子標籤加入即可。

  如:

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>
複製代碼

 

  其中android:name屬性填上你自己創建的fragment的完整類名。

  當系統創建這個Activity的佈局文件時,系統會實例化每一個fragment,並且調用它們的onCreateView()方法,來獲得相應fragment的佈局,並將返回值插入fragment標籤所在的地方。

  有三種方法爲Fragment提供ID:

  android:id屬性:唯一的id

  android:tag屬性:唯一的字符串

  如果上面兩個都沒提供,系統使用容器view的ID。

 

加載方式2:通過編程的方式將Fragment加入到一個ViewGroup中

  當Activity處於Running狀態下的時候,可以在Activity的佈局中動態地加入Fragment,只需要指定加入這個Fragment的父View Group即可。

  首先,需要一個FragmentTransaction實例: 

FragmentManager fragmentManager = getFragmentManager()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

  (注,如果import android.support.v4.app.FragmentManager;那麼使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)

  

  之後,用add()方法加上Fragment的對象:

ExampleFragment fragment = new ExampleFragment();

fragmentTransaction.add(R.id.fragment_container, fragment);

fragmentTransaction.commit();

 

  其中第一個參數是這個fragment的容器,即父控件組。

  最後需要調用commit()方法使得FragmentTransaction實例的改變生效。

 

實例

  練習的例子:

  寫一個類繼承自Fragment類,並且寫好其佈局文件(本例中是兩個TextView),在Fragment類的onCreateView()方法中加入該佈局。

  之後用兩種方法在Activity中加入這個fragment:

  第一種是在Activity的佈局文件中加入<fragment>標籤;

  第二種是在Activity的代碼中使用FragmentTransaction的add()方法加入fragment。

  貼出代碼:

  自己定義的fragment類: 

複製代碼
package com.example.learningfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class ExampleFragment extends Fragment
{

    //三個一般必須重載的方法
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("ExampleFragment--onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        System.out.println("ExampleFragment--onCreateView");
        return inflater.inflate(R.layout.example_fragment_layout, container, false);
        
    }

    @Override
    public void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("ExampleFragment--onPause");
    }

    

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        System.out.println("ExampleFragment--onResume");
    }

    @Override
    public void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();
        System.out.println("ExampleFragment--onStop");
    }
    
    

}
複製代碼

  fragment的佈局文件:

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/num1"
           />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/num2"
        />  
    

</LinearLayout>
複製代碼

  主Activity:

複製代碼
package com.example.learningfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


public class LearnFragment extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learn_fragment);
        
        
        //在程序中加入Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        
        ExampleFragment fragment = new ExampleFragment();
        fragmentTransaction.add(R.id.linear, fragment);
        fragmentTransaction.commit();
    }

}
複製代碼

  Activity的佈局文件:

複製代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn1"      
        />

    <fragment 
        android:name="com.example.learningfragment.ExampleFragment"
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn2"      
        />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
     <Button
        android:id="@+id/btn3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn3"      
        />
     </LinearLayout>

</LinearLayout>
複製代碼

 

  運行結果如下:

 

 

 

 

  可以看到第二種方式加入fragment的時候,指定了父容器(一個線性佈局)的id,其中已經有一個Button 3,所以fragment加在其後。


Fragment生命週期

官網幫助文檔鏈接:
 http://developer.android.com/guide/components/fragments.html

主要看兩張圖,和跑代碼

一,Fragment的生命周 

          

二,與Activity生命週期的對比

     

場景演示 : 切換到該Fragment

11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume

屏幕滅掉:

11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop


屏幕解鎖

11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume


切換到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView


切換回本身的Fragment:

11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume

回到桌面

11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop

回到應用

11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume


退出應用

11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach


比Activity多了一些生命週期,完整和Activity對接上,大家好好利用。



 


發佈了3 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章