1. 程式人生 > >Fragment生命周期詳解

Fragment生命周期詳解

開啟事務 rem destroy ide edi on() 進行 nco ===

Fragment依附Activity而存在,本文通過兩個TextView切換兩個Fragment來觀察Activity和兩個Fragment在不同操作下各執行哪些方法,從而對其生命周期有更加深刻的了解。通過打印log進行分析。

  • 本文要點:
  1. Fragment的動態加載,和切換。
  2. Fragment和Activity的生命周期分析。
  • 註:文中:
  1. 使用ButterKnife,免去findViewById。
  2. LogUtils.e()為個人工具類,可等視為log.e()
  • 進入LifeActivity時,Fragment1顯示:
  1. 先執行LifeActivity onCreate
    方法,再執行Fragment1onAttachonAttach、onCreateView、onActivityCreated方法。
  2. LifeActivity、Fragment1 onStart方法LifeActivity、Fragment1 onResume方法 可見在初始化時Activity要優先Fragment一步。

03-29 23:09:43.827 14564-14564/top.toly.www.fragment E/LifeActivity: onCreate: ====LifeActivity
03-29 23:09:43.831 14564-14564/top.toly.www.fragment E/Fragment1: onAttach: ====Fragment1
03-29 23:09:43.831 14564-14564/top.toly.www.fragment E/Fragment1: onCreate: ====Fragment1
03-29 23:09:43.831 14564-14564/top.toly.www.fragment E/Fragment1: onCreateView:====Fragment1 
03-29 23:09:43.832 14564-14564/top.toly.www.fragment E/Fragment1: onActivityCreated:====Fragment1 
03-29 23:09:43.832 14564-14564/top.toly.www.fragment E/LifeActivity: onStart: ====LifeActivity
03-29 23:09:43.832 14564-14564/top.toly.www.fragment E/Fragment1: onStart: ====Fragment1
03-29 23:09:43.832 14564-14564/top.toly.www.fragment E/LifeActivity: onResume: ====LifeActivity
03-29 23:09:43.832 14564-14564/top.toly.www.fragment E/Fragment1: onResume:====Fragment1

  • 將屏幕關閉時:Fragment1LifeActivity分別執行onPauseonStop方法。 可見在暫停時Fragment要優先Activity一步。
03-29 23:17:12.928 19745-19745/top.toly.www.fragment E/Fragment1: onPause:====Fragment1
03-29 23:17:12.928 19745-19745/top.toly.www.fragment E/LifeActivity: onPause: ====LifeActivity
03-29 23:17:12.960 19745-19745/top.toly.www.fragment E/Fragment1: onStop: ====Fragment1
03-29 23:17:12.960 19745-19745/top.toly.www.fragment E/LifeActivity: onStop: ====LifeActivity

  • 將屏幕打開時:LifeActivityonRestartLifeActivityFragment1onStart。LifeActivityFragment1onResume
03-29 23:17:59.144 19745-19745/top.toly.www.fragment E/LifeActivity: onRestart: ====LifeActivity
03-29 23:17:59.153 19745-19745/top.toly.www.fragment E/LifeActivity: onStart: ====LifeActivity
03-29 23:17:59.153 19745-19745/top.toly.www.fragment E/Fragment1: onStart: ====Fragment1
03-29 23:17:59.154 19745-19745/top.toly.www.fragment E/LifeActivity: onResume: ====LifeActivity
03-29 23:17:59.154 19745-19745/top.toly.www.fragment E/Fragment1: onResume:====Fragment1

  • Fragment1切到Fragment2時:
03-29 23:20:49.003 22825-22825/top.toly.www.fragment E/Fragment1: onPause:====Fragment1 
03-29 23:20:49.005 22825-22825/top.toly.www.fragment E/Fragment1: onStop: ====Fragment1
03-29 23:20:49.005 22825-22825/top.toly.www.fragment E/Fragment1: onDestroyView: ====Fragment1
03-29 23:20:49.005 22825-22825/top.toly.www.fragment E/Fragment1: onDestroy:====Fragment1 
03-29 23:20:49.006 22825-22825/top.toly.www.fragment E/Fragment1: onDetach:====Fragment1 
03-29 23:20:49.006 22825-22825/top.toly.www.fragment E/Fragment2: onAttach: ====Fragment2
03-29 23:20:49.006 22825-22825/top.toly.www.fragment E/Fragment2: onCreate: ====Fragment2
03-29 23:20:49.006 22825-22825/top.toly.www.fragment E/Fragment2: onCreateView:====Fragment2 
03-29 23:20:49.035 22825-22825/top.toly.www.fragment E/Fragment2: onActivityCreated:====Fragment2 
03-29 23:20:49.035 22825-22825/top.toly.www.fragment E/Fragment2: onStart: ====Fragment2
03-29 23:20:49.035 22825-22825/top.toly.www.fragment E/Fragment2: onResume:====Fragment2 

  • Fragment2切到Fragment1時:

03-29 23:22:18.835 22825-22825/top.toly.www.fragment E/Fragment2: onPause:====Fragment2 
03-29 23:22:18.835 22825-22825/top.toly.www.fragment E/Fragment2: onStop: ====Fragment2
03-29 23:22:18.835 22825-22825/top.toly.www.fragment E/Fragment2: onDestroyView: ====Fragment2
03-29 23:22:18.836 22825-22825/top.toly.www.fragment E/Fragment2: onDestroy:====Fragment2 
03-29 23:22:18.836 22825-22825/top.toly.www.fragment E/Fragment2: onDetach:====Fragment2 
03-29 23:22:18.837 22825-22825/top.toly.www.fragment E/Fragment1: onAttach: ====Fragment1
03-29 23:22:18.837 22825-22825/top.toly.www.fragment E/Fragment1: onCreate: ====Fragment1
03-29 23:22:18.837 22825-22825/top.toly.www.fragment E/Fragment1: onCreateView:====Fragment1 
03-29 23:22:18.840 22825-22825/top.toly.www.fragment E/Fragment1: onActivityCreated:====Fragment1 
03-29 23:22:18.840 22825-22825/top.toly.www.fragment E/Fragment1: onStart: ====Fragment1
03-29 23:22:18.840 22825-22825/top.toly.www.fragment E/Fragment1: onResume:====Fragment1 


  • 退出LifeActivity時:
03-29 23:11:47.792 14564-14564/top.toly.www.fragment E/Fragment1: onPause:====Fragment1 
03-29 23:11:47.792 14564-14564/top.toly.www.fragment E/LifeActivity: onPause: ====LifeActivity
03-29 23:11:47.798 14564-14564/top.toly.www.fragment E/Fragment1: onStop: ====Fragment1
03-29 23:11:47.798 14564-14564/top.toly.www.fragment E/LifeActivity: onStop: ====LifeActivity
03-29 23:11:47.798 14564-14564/top.toly.www.fragment E/Fragment1: onDestroyView: ====Fragment1
03-29 23:11:47.799 14564-14564/top.toly.www.fragment E/Fragment1: onDestroy:====Fragment1 
03-29 23:11:47.799 14564-14564/top.toly.www.fragment E/Fragment1: onDetach:====Fragment1 
03-29 23:11:47.799 14564-14564/top.toly.www.fragment E/LifeActivity: onDestroy: ====LifeActivity
  • 下面是具體代碼。(源碼地址:https://github.com/toly1994328/Fragment.git)

LifeActivity.java

 1 package top.toly.www.fragment.life;
 2 
 3 import android.app.FragmentManager;
 4 import android.app.FragmentTransaction;
 5 import android.os.Bundle;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.view.View;
 8 import android.widget.FrameLayout;
 9 import android.widget.TextView;
10 
11 import butterknife.Bind;
12 import butterknife.ButterKnife;
13 import butterknife.OnClick;
14 import top.toly.www.fragment.R;
15 import utils.shortUtils.LogUtils;
16 
17 public class LifeActivity extends AppCompatActivity {
18     private static final String TAG = "LifeActivity";
19 
20     @Bind(R.id.tv_title1)
21     TextView mTvTitle1;
22     @Bind(R.id.tv_title2)
23     TextView mTvTitle2;
24     @Bind(R.id.fl_life_root)
25     FrameLayout mFlLifeRoot;
26     private FragmentManager mFm;
27     private FragmentTransaction mFt;
28 
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_life);
33         LogUtils.e(TAG, "onCreate: ====LifeActivity");
34         ButterKnife.bind(this);
35         initFragment();//初始Fragment
36     }
37 
38     @Override
39     protected void onStart() {
40         super.onStart();
41         LogUtils.e(TAG, "onStart: ====LifeActivity");
42     }
43 
44     @Override
45     protected void onResume() {
46         super.onResume();
47         LogUtils.e(TAG, "onResume: ====LifeActivity");
48     }
49 
50     @Override
51     protected void onPause() {
52         super.onPause();
53         LogUtils.e(TAG, "onPause: ====LifeActivity");
54     }
55 
56     @Override
57     protected void onStop() {
58         super.onStop();
59         LogUtils.e(TAG, "onStop: ====LifeActivity");
60     }
61 
62     @Override
63     protected void onRestart() {
64         super.onRestart();
65         LogUtils.e(TAG, "onRestart: ====LifeActivity");
66     }
67     @Override
68     protected void onDestroy() {
69         super.onDestroy();
70         LogUtils.e(TAG, "onDestroy: ====LifeActivity");
71     }
72 
73     private void initFragment() {
74         mFm = getFragmentManager();//1.獲取FragmentManager
75         mFt = mFm.beginTransaction();//2.fm開啟事務
76         mFt.add(R.id.fl_life_root, new Fragment1()); //3.動態添加
77         mFt.commit();//4.提交事務
78     }
79 
80     @OnClick({R.id.tv_title1, R.id.tv_title2})
81     public void onViewClicked(View view) {
82         mFt = mFm.beginTransaction();//2.fm開啟事務
83         switch (view.getId()) {
84             case R.id.tv_title1:
85                 mFlLifeRoot.removeAllViews();//先清屏
86                 mFt.replace(R.id.fl_life_root, new Fragment1()); //3.動態添加
87                 break;
88             case R.id.tv_title2:
89                 mFlLifeRoot.removeAllViews();//先清屏
90                 mFt.replace(R.id.fl_life_root, new Fragment2()); //3.動態添加
91                 break;
92         }
93         mFt.commit();//4.提交事務
94     }
95 }

Fragment1.java

 1 package top.toly.www.fragment.life;
 2 
 3 
 4 import android.app.Fragment;
 5 import android.content.Context;
 6 import android.os.Bundle;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 
11 import top.toly.www.fragment.R;
12 import utils.shortUtils.LogUtils;
13 
14 /**
15  * A simple {@link Fragment} subclass.
16  */
17 public class Fragment1 extends Fragment {
18     private static final String TAG = "Fragment1";
19 
20     public Fragment1() {
21         // Required empty public constructor
22     }
23 
24     @Override
25     public void onAttach(Context context) {
26         super.onAttach(context);
27         LogUtils.e(TAG, "onAttach: ====Fragment1");
28     }
29 
30     @Override
31     public void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         LogUtils.e(TAG, "onCreate: ====Fragment1");
34     }
35 
36     @Override
37     public View onCreateView(LayoutInflater inflater, ViewGroup container,
38                              Bundle savedInstanceState) {
39         LogUtils.e(TAG, "onCreateView:====Fragment1 ");
40         return inflater.inflate(R.layout.fragment_fragment1, container, false);
41     }
42 
43     @Override
44     public void onActivityCreated(Bundle savedInstanceState) {
45         super.onActivityCreated(savedInstanceState);
46         LogUtils.e(TAG, "onActivityCreated:====Fragment1 ");
47     }
48 
49     @Override
50     public void onStart() {
51         super.onStart();
52         LogUtils.e(TAG, "onStart: ====Fragment1");
53     }
54 
55     @Override
56     public void onResume() {
57         super.onResume();
58         LogUtils.e(TAG, "onResume:====Fragment1 ");
59     }
60 
61     @Override
62     public void onPause() {
63         super.onPause();
64         LogUtils.e(TAG, "onPause:====Fragment1 ");
65     }
66 
67     @Override
68     public void onStop() {
69         super.onStop();
70         LogUtils.e(TAG, "onStop: ====Fragment1");
71     }
72 
73     @Override
74     public void onDestroyView() {
75         super.onDestroyView();
76         LogUtils.e(TAG, "onDestroyView: ====Fragment1");
77     }
78 
79     @Override
80     public void onDestroy() {
81         super.onDestroy();
82         LogUtils.e(TAG, "onDestroy:====Fragment1 ");
83     }
84 
85     @Override
86     public void onDetach() {
87         super.onDetach();
88         LogUtils.e(TAG, "onDetach:====Fragment1 ");
89     }
90 }

Fragment2.java

 1 package top.toly.www.fragment.life;
 2 
 3 
 4 import android.app.Fragment;
 5 import android.content.Context;
 6 import android.os.Bundle;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 
11 import top.toly.www.fragment.R;
12 import utils.shortUtils.LogUtils;
13 
14 /**
15  * A simple {@link Fragment} subclass.
16  */
17 public class Fragment2 extends Fragment {
18     private static final String TAG = "Fragment2";
19 
20     public Fragment2() {
21         // Required empty public constructor
22     }
23 
24     @Override
25     public void onAttach(Context context) {
26         super.onAttach(context);
27         LogUtils.e(TAG, "onAttach: ====Fragment2");
28     }
29 
30     @Override
31     public void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         LogUtils.e(TAG, "onCreate: ====Fragment2");
34     }
35 
36     @Override
37     public View onCreateView(LayoutInflater inflater, ViewGroup container,
38                              Bundle savedInstanceState) {
39         LogUtils.e(TAG, "onCreateView:====Fragment2 ");
40         return inflater.inflate(R.layout.fragment_fragment2, container, false);
41     }
42 
43     @Override
44     public void onActivityCreated(Bundle savedInstanceState) {
45         super.onActivityCreated(savedInstanceState);
46         LogUtils.e(TAG, "onActivityCreated:====Fragment2 ");
47     }
48 
49     @Override
50     public void onStart() {
51         super.onStart();
52         LogUtils.e(TAG, "onStart: ====Fragment2");
53     }
54 
55     @Override
56     public void onResume() {
57         super.onResume();
58         LogUtils.e(TAG, "onResume:====Fragment2 ");
59     }
60 
61     @Override
62     public void onPause() {
63         super.onPause();
64         LogUtils.e(TAG, "onPause:====Fragment2 ");
65     }
66 
67     @Override
68     public void onStop() {
69         super.onStop();
70         LogUtils.e(TAG, "onStop: ====Fragment2");
71     }
72 
73     @Override
74     public void onDestroyView() {
75         super.onDestroyView();
76         LogUtils.e(TAG, "onDestroyView: ====Fragment2");
77     }
78 
79     @Override
80     public void onDestroy() {
81         super.onDestroy();
82         LogUtils.e(TAG, "onDestroy:====Fragment2 ");
83     }
84 
85     @Override
86     public void onDetach() {
87         super.onDetach();
88         LogUtils.e(TAG, "onDetach:====Fragment2 ");
89     }
90 }

LifeActivity的布局文件:activity_life.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="top.toly.www.fragment.life.LifeActivity">
<LinearLayout
    android:id="@+id/ll_life_root"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tv_title1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="title1"
        android:gravity="center"/>
    <TextView
        android:id="@+id/tv_title2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="title2"
        android:gravity="center"/>
</LinearLayout>
    <FrameLayout
        android:id="@+id/fl_life_root"
        android:layout_below="@+id/ll_life_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</RelativeLayout>

Fragment1使用的布局文件:fragment_fragment1.xml

<RelativeLayout 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"
             tools:context="top.toly.www.fragment.life.Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textSize="24sp"
        android:text="@string/fragment1"/>
</RelativeLayout>

Fragment2使用的布局文件:fragment_fragment2.xml

<RelativeLayout 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"
             tools:context="top.toly.www.fragment.life.Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textSize="24sp"
        android:text="@string/fragment2"/>

</RelativeLayout>

Fragment生命周期詳解