Android Fragment源碼分析Add方法

前言

本篇我們就來講講Fragment管理中的 Add() 方法

Add()

在我們動態的添加、管理Fragment中,Add屬於最基礎的方法瞭; 用法也很簡單,如下就是向Activity添加一個Fragment:

getSupportFragmentManager().beginTransaction().add(R.id.fragmenta,new FragmentA()).commit();

一般時候我們使用到Fragment的時候,都是不止一個,比如微信界面,底部導航有四個按鈕,分別對應不同的四個Fragment,像這種的每點擊一次底部按鈕就切換一下界面的話,我們就可以使用Add()外加hide和show進行組合

下面我們簡單實現一下,這裡我們就弄兩個Fragment,

這裡我們的MainActivity的佈局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.shaoen.lenovo.myapplication.MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/fragmenta"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/fragmenta_button" 
            android:text="FragmentA"
            android:layout_width="0dp"
            android:layout_height="match_parent" 
            android:layout_weight="1"/>
        <Button
            android:id="@+id/fragmentb_button"
            android:text="FragmentB"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

下面看MainActivity的內容:

package com.shaoen.lenovo.myapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.shaoen.lenovo.myapplication.fragment.FragmentA;
import com.shaoen.lenovo.myapplication.fragment.FragmentB;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private String  TAG=MainActivity.class.getSimpleName();
    private Button fragmentA_Button;
    private Button fragmentB_Button;
private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private Fragment fragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate--執行瞭");
        setContentView(R.layout.activity_main);
        fragmentManager=getSupportFragmentManager();
        transaction=  fragmentManager.beginTransaction();
        fragment=new FragmentA();
        transaction.add(R.id.fragmenta,fragment,"FragmentA").commit();
        fragmentA_Button=(Button) findViewById(R.id.fragmenta_button);
        fragmentB_Button=(Button) findViewById(R.id.fragmentb_button);
        fragmentA_Button.setOnClickListener(this);
        fragmentB_Button.setOnClickListener(this);
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG,"onStart--執行瞭");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG,"onResume--執行瞭");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG,"onPause--執行瞭");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG,"onStop--執行瞭");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"onDestroy--執行瞭");
    }
    @Override
    public void onClick(View v) {
        transaction=  fragmentManager.beginTransaction();
        switch (v.getId()){
            case R.id.fragmenta_button:
                if (fragment!=null)
                    transaction.hide(fragment);
                fragment=  fragmentManager.findFragmentByTag("FragmentA");
                if (fragment!=null){
                    transaction.show(fragment);
                }
                else {
                    fragment=new FragmentA();
                    transaction.add(R.id.fragmenta,fragment,"FragmentA").commit();
                }
                break;
           case R.id.fragmentb_button:
                if (fragment!=null)
                    transaction.hide(fragment);
            fragment=  fragmentManager.findFragmentByTag("FragmentB");
                if (fragment!=null){
                    transaction.show(fragment);
                }
                else {
                    fragment=new FragmentB();
                    transaction.add(R.id.fragmenta,fragment,"FragmentB").commit();
                }
                break;
        }
    }
}

這裡我們寫的比較簡單,主要是為瞭看一下他們的執行生命周期,在這裡我把所以log都打印出來瞭

剛開始運行時的log如下:

I/MainActivity: onCreate–執行瞭
I/FragmentA: onAttach–執行瞭
I/FragmentA: onCreate–執行瞭
I/FragmentA: onCreateView–執行瞭
I/FragmentA: onActivityCreated–執行瞭
I/FragmentA: onStart–執行瞭
I/MainActivity: onStart–執行瞭
I/MainActivity: onResume–執行瞭
I/FragmentA: onResume–執行瞭

此時我們點擊FragmentB按鈕;

I/FragmentB: onAttach–執行瞭
I/FragmentB: onCreate–執行瞭
I/FragmentB: onCreateView–執行瞭
I/FragmentB: onActivityCreated–執行瞭
I/FragmentB: onStart–執行瞭
I/FragmentB: onResume–執行瞭

然後我們在反復點擊FragmentA和FragmentB按鈕,發現沒有任何log打印,此時證明FragmentA和FragmentB通過hide和show方法進行切換時,都隻會初始化一次,

下面我們看向replace這個方法

replace:

首先replace方法,其實是remove和add方法的組合; remove就是將一個Fragment從FragmentManager中刪除,如果我們切換下一個Fragment時,上一個Fragment不需要瞭,可以直接使用replace,如果我們還需要的話,API中也提供瞭相應的方法,那就是加入回退棧addToBackStack()

下面我們把MainActivity中的代碼改一下:

  @Override
    public void onClick(View v) {
        transaction=  fragmentManager.beginTransaction();
        switch (v.getId()){
            case R.id.fragmenta_button:
                if (fragment!=null)
                    transaction.hide(fragment);
                fragment=  fragmentManager.findFragmentByTag("FragmentA");
                if (fragment!=null){ Log.i(TAG,"fragment不為空");
                    transaction.show(fragment);
                }
                else {
                    Log.i(TAG,"fragment為空");
                    fragment=new FragmentA();
                    transaction.replace(R.id.fragmenta,fragment,"FragmentA").addToBackStack("FragmentA").commit();
                }
                break;
           case R.id.fragmentb_button:
                if (fragment!=null)
                    transaction.hide(fragment);
            fragment=  fragmentManager.findFragmentByTag("FragmentB");
                if (fragment!=null){
                    Log.i(TAG,"fragment不為空");
                    transaction.show(fragment);
                }
                else {
                    Log.i(TAG,"fragment為空");
                    fragment=new FragmentB();
                    transaction.replace(R.id.fragmenta,fragment,"FragmentB").addToBackStack("FragmentB").commit();
                }
                break;
        }
        }

這裡我們就改瞭一下OnClick中的代碼,這時我們再打印一下log看看:

首先初始化時是一致的:

這裡寫代碼片

此時我們點擊FragmentB:

12-18 21:48:14.227 21081-21081/com.shaoen.lenovo.myapplication I/MainActivity: fragment為空
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onPause–執行瞭
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onStop–執行瞭
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onDestroyView–執行瞭
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onAttach–執行瞭
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreate–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreateView–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onActivityCreated–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onStart–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onResume–執行瞭

我們發現Fragment調用瞭destroy方法,此時我們再點擊FragmentA:

I/MainActivity: fragment不為空

此時發現FragmentA沒有切換過來,這是因為,我們在FragmentManager中找到瞭FragmentA的實例,但是此時,FragmentA的界面已經被銷毀瞭,所以我們看見的還是FragmentB,此時我們的OnClick改成如下:

   @Override
    public void onClick(View v) {
        transaction=  fragmentManager.beginTransaction();
        switch (v.getId()){
            case R.id.fragmenta_button:
                    fragment=new FragmentA();
                    transaction.replace(R.id.fragmenta,fragment,"FragmentA").addToBackStack("FragmentA").commit();
                break;
           case R.id.fragmentb_button:
                    fragment=new FragmentB();
                    transaction.replace(R.id.fragmenta,fragment,"FragmentB").addToBackStack("FragmentB").commit();
                break;
        }
    }

這時再打印一下log,

12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onPause–執行瞭
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onStop–執行瞭
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onDestroyView–執行瞭
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onAttach–執行瞭
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreate–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreateView–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onActivityCreated–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onStart–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onResume–執行瞭

12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onPause–執行瞭
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onStop–執行瞭
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onDestroyView–執行瞭
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onAttach–執行瞭
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onCreate–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onCreateView–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onActivityCreated–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onStart–執行瞭
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onResume–執行瞭

此時發現每次切換時,都會調用Fragment都會重新調用onCreateView()到onDestroyView()的所有方法,其實就是Fragment的佈局層整個銷毀到重建的過程

註: 當我們進行Fragment嵌套時,如果我們點擊返回鍵,不想回到上一個Fragment,而想直接回到更往前一個,或者更往前的Fragment,我們可以使用FragmentManager.popBackStackImmediate (String tag, int flags)方法,彈出TAG為tag的Fragment,同時把此Fragment以上的Fragment全都彈出(彈出回退棧,即徹底銷毀,detach)

到此這篇關於Android Fragment源碼分析Add方法的文章就介紹到這瞭,更多相關Android Fragment內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: