1. 程式人生 > >Fragment生命週期及add、replace、remove、hide&show方法分析

Fragment生命週期及add、replace、remove、hide&show方法分析

最近做專案時,發現未能完全的理解Fragment的生命週期,以致在處理邏輯上踩到到了一些坑。所以花了點時間去分析,也順便整理下,下次再碰到Fragment時就可以避免一些坑了(大笑)。

專案有一個Activity類和3個Fragment類。Activity 中放置3個按鈕(實質TextView)可以切換到不同的Fragment上。下面將給出Activity 和Fragment的程式碼和layout。

1-1 Fragment1

public class Fragment1 extends Fragment {
    final  String TAG  = " test  Fragment1"
; public Fragment1() { // Required empty public constructor } @Override public void onAttach(Context context) { super.onAttach(context); Log.d(TAG,"onAttach"); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"onCreate"
); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1_layout, null); Log.d(TAG,"onCreateView"); return view; } @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d(TAG,"onActivityCreated"); } @Override public void onStart() { super.onStart(); Log.d(TAG,"onStart"); } @Override public void onResume() { super.onResume(); Log.d(TAG,"onResume"); } @Override public void onPause() { super.onPause(); Log.d(TAG,"onPause"); } @Override public void onStop() { super.onStop(); Log.d(TAG,"onStop"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG,"onDestroyView"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy"); } @Override public void onDetach() { super.onDetach(); Log.d(TAG,"onDetach"); } }

1-2 fragment1_layout.xml

<?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:background="#888"
    android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Fragment1"/>
</LinearLayout>

2-1 Fragment2

public class Fragment2 extends Fragment {
    final  String TAG  = " test  Fragment2";

    public Fragment2() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG,"onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2_layout, null);
        Log.d(TAG,"onCreateView");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG,"onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG,"onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG,"onDetach");
    }
}

2-2 fragment2_layout.xml

<?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:background="#888"
    android:gravity="center">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:text="Fragment2"/>
</LinearLayout>

3-1 Fragment3

public class Fragment3 extends Fragment {
    final  String TAG  = " test  Fragment3";

    public Fragment3() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG,"onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment3_layout, null);
        Log.d(TAG,"onCreateView");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG,"onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG,"onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG,"onDetach");
    }
}

3-2 fragment3_layout.xml

<?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:background="#888"
    android:gravity="center">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:text="Fragment3"/>
</LinearLayout>

4-1 MainActivity

public class MainActivity extends Activity{
    final String TAG = " test  MainActivity";

    TextView mFragment1Tv ;
    TextView mFragment2Tv ;
    TextView mFragment3Tv ;

    FrameLayout mainll ;

    FragmentManager manager ;
    FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        initView();
        initListener();
        initData();

        Log.d(TAG,"onCreate");
    }

    private void initListener() {
        mFragment1Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment1 fragment1= new Fragment1();
                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment1,"1");
                transaction.commit();
            }
        });
        mFragment2Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment2 fragment2= new Fragment2();
                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment2,"2");
                transaction.commit();
            }
        });
        mFragment3Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment3 fragment3= new Fragment3();
                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment3,"3");
                transaction.commit();
            }
        });
    }

    private void initData() {
        manager = getFragmentManager();
    }

    private void initView() {
        mFragment1Tv = (TextView) findViewById(R.id.fragment1_tv);
        mFragment2Tv = (TextView) findViewById(R.id.fragment2_tv);
        mFragment3Tv = (TextView) findViewById(R.id.fragment3_tv);
        mainll  = (FrameLayout) findViewById(R.id.main_ll);

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"onCreate");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG,"onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
}

4-2 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/index"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/fragment1_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#bbb"
            android:text="Fragment1"
            android:textSize="22sp" />

        <TextView
            android:id="@+id/fragment2_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#bbb"
            android:text="Fragment2"
            android:textSize="22sp" />

        <TextView
            android:id="@+id/fragment3_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#bbb"
            android:text="Fragment3"
            android:textSize="22sp" />
    </LinearLayout>
    <View
        android:layout_below="@id/index"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#aaa"/>

    <LinearLayout
        android:id="@+id/main_ll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">
    </LinearLayout>


</RelativeLayout>

5 測試生命週期
(一) add 方法分析
1.啟動app,log如圖:
這裡寫圖片描述
介面如下:
這裡寫圖片描述

2.點選Fragment1,log如下:
這裡寫圖片描述

介面如下:
這裡寫圖片描述

3點選fragment2 log如下: (這裡並沒有觸發fragment1的生命週期)

這裡寫圖片描述
介面如下:
這裡寫圖片描述

4點選fragment3 log如下:
這裡寫圖片描述
介面如下:
這裡寫圖片描述

5 在此時分為兩次不同的操作:

5-1 測試返回
操作:點選返回鍵,此時應用已經退出。log如下:
這裡寫圖片描述

5-2 home鍵測試
操作:(1)點選home鍵,app退到後臺,log如下:
這裡寫圖片描述

(2)點選應用圖示重新開啟app,log如下:

這裡寫圖片描述

6 回退棧測試
再回到5-1 ,如果想要點選返回鍵回到上一個Fragment的話,那麼就需要加入到會退棧。
修改 MainActivity 的方法initListener如下:

private void initListener() {
        mFragment1Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment1 fragment1= new Fragment1();
                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment1,"1");
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
        mFragment2Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment2 fragment2= new Fragment2();
                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment2,"2");
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
        mFragment3Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment3 fragment3= new Fragment3();
                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment3,"3");
                transaction.addToBackStack(null);
                transaction.commit();

            }
        });
    }

操作:執行 1.開啟app,2.點選Fragment,3.點選Fragment2,4.點選Fragment3。
此時清空LogCat,再點選返回鍵Log如下:

這裡寫圖片描述
此時介面回到Fragment2,而其他Fragment和Activity的生命週期是沒有變化的。若再點選返回鍵將回到Fragment1介面,再點選返回鍵就是回到Activity介面了。

(二) replace方法分析
修改 MainActivity 的方法initListener如下:(主要是替換add為replace)

private void initListener() {
        mFragment1Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment1 fragment1= new Fragment1();
                transaction = manager.beginTransaction();
                transaction.replace(R.id.main_ll, fragment1,"1");
               //transaction.add(R.id.main_ll, fragment1,"1");
             // transaction.addToBackStack(null);
                transaction.commit();
            }
        });
        mFragment2Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment2 fragment2= new Fragment2();
                transaction = manager.beginTransaction();
                transaction.replace(R.id.main_ll, fragment2,"2");
                //transaction.add(R.id.main_ll, fragment2,"2");
              // transaction.addToBackStack(null);
                transaction.commit();
            }
        });
        mFragment3Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment3 fragment3= new Fragment3();
                transaction = manager.beginTransaction();
                transaction.replace(R.id.main_ll, fragment3,"3");
                //transaction.add(R.id.main_ll, fragment3,"3");
                //transaction.addToBackStack(null);
                transaction.commit();

            }
        });
    }

1.測試replace
操作:啟動app,然後點選Fragment1,再點選Fragment2,log如下:
這裡寫圖片描述

2.測試返回鍵
在上一步驟的基礎上點選物理返回鍵Log如下:(應用程式已經退出了)
這裡寫圖片描述

3.在(二)1的基礎上點選home鍵,再重新開啟app,log如下:
這裡寫圖片描述

4.此時看下會退棧的情況。
修改 MainActivity 的方法initListener,主要是取消

// transaction.addToBackStack(null);

的註釋。

操作:開啟app,點選Fragment1,點選Fragment2,清空logCat,再點選返回鍵,此時log如下:
這裡寫圖片描述
可見replace 是會銷燬掉被replace 的Fragment。

(三) hide &show方法分析。
修改 MainActivity如下:(在activity的oncreate中add Fragment1和Fragment2,修改mFragment2Tv的點選事件。)

public class MainActivity extends Activity{
    final String TAG = "testFragment  MainActivity";

    TextView mFragment1Tv ;
    TextView mFragment2Tv ;
    TextView mFragment3Tv ;

    FrameLayout mainll ;

    FragmentManager manager ;
    FragmentTransaction transaction;

    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        initView();
        initListener();
        initData();

        Log.d(TAG,"onCreate");
    }

    private void initListener() {
        mFragment1Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                transaction = manager.beginTransaction();
                transaction.add(R.id.main_ll, fragment1,"1");
                transaction.commit();
            }
        });
        mFragment2Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction = manager.beginTransaction();
                transaction.hide(fragment1).show(fragment2);
                transaction.commit();
            }
        });
        mFragment3Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                transaction = manager.beginTransaction();
                transaction.replace(R.id.main_ll, fragment3,"3");
//                transaction.add(R.id.main_ll, fragment3,"3");
                transaction.addToBackStack(null);
                transaction.commit();

            }
        });
    }

    private void initData() {
        manager = getFragmentManager();
        fragment1= new Fragment1();
        fragment2= new Fragment2();
        fragment3= new Fragment3();
        transaction = manager.beginTransaction();
        transaction.add(R.id.main_ll, fragment2,"2");
        transaction.add(R.id.main_ll, fragment1,"1");
        transaction.commit();
    }

    private void initView() {
        mFragment1Tv = (TextView) findViewById(R.id.fragment1_tv);
        mFragment2Tv = (TextView) findViewById(R.id.fragment2_tv);
        mFragment3Tv = (TextView) findViewById(R.id.fragment3_tv);
        mainll  = (FrameLayout) findViewById(R.id.main_ll);

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG,"onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
}

操作:啟動app ,log如下圖,程式碼裡面是先add Fragment2 再add Fragment1,此時顯示的介面是Fragment1 的介面,然後點選TextView mFragment2Tv,介面切換到了Fragment2上,而log無任何的資訊列印,可見hide &show 方法不觸發Fragment生命週期,隻影響它的顯示。
這裡寫圖片描述

(四) remove方法分析
在 (三)hide&show方法分析 的程式碼基礎上修改MainActivity 如下:
(其他程式碼不變,只修改為remove方法)

mFragment2Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction = manager.beginTransaction();
                transaction.remove(fragment1).show(fragment2);
                transaction.commit();
            }
        });

啟動app 時的log:
這裡寫圖片描述
此時點選Fragment2,log如下:
這裡寫圖片描述

本以為分析下Fragment會很簡單,想不到分析Fragment+一篇文章 能耗那麼多時間,看來得加個滷蛋犒勞犒勞自己了(偷笑中…)