標籤:
上一篇我們介紹了Fragment的簡單應用以及通過Fragment來解決橫豎屏切換的情況下顯示不同介面,通過Fragment填充多餘空間的問題,這一篇我們介紹下Fragment生命週期;
眾所周知,Fragment是可以作為"小Activity"的,嵌套在Activity中使用,因此他的生命週期和Activity是分不開的,只有當Activity處於活動狀態的情況下才能進行Fragment各個生命週期活動的變化,Activity銷毀的話,綁定在他上面的Fragment隨之也會銷毀;
Fragment的生命週期所涉及到的方法有:
(1) onAttach(): 當Fragment被添加到Activity的時候被調用;
(2) onCreate(): 建立Fragment的時候被調用,該方法之後被調用一次;
(3) onCreateView(): 這是我們經常會覆寫的一個方法,主要是為了繪製Fragment上的View組件顯示問題,Fragment顯示的View就是該方法的返回值;
(4) onActivityCreated(): 當Fragment所在的Activity被被啟動的時候調用;
(5) onStart(): Fragment啟動的時候調用,類似於Activity的onStart();
(6) onResume(): 恢複Fragment的時候調用,onStart方法之後一定會回調onResume()方法
(7) onPause(): Fragment暫停時候調用,類似於Activity的onPause();
(8) onStop(): Fragment停止的時候調用,類似於Activity的onStop();
(9) onDestroyView(): 銷毀Fragment上面顯示的View的時候回調的方法;
(10) onDestroy(): 銷毀Fragment的時候調用的方法,類似於Activity的onDestroy()方法;
(11) onDetach(): 當Fragment與Activity解除綁定的時候調用,onDestroy方法之後一定會執行onDetach方法,並且只會執行一次;
可以注意到的一點就是Fragment的生命週期中沒有onRestart()方法,這也是我最詫異的地方,希望大家知道的留言給我,下面以執行個體來學習Fragment的生命週期:
定義一個Fragment布局檔案:fragment1.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:orientation="vertical" android:background="#ffff00"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/fragment1"/></LinearLayout>
定義Fragment類FragmentLiferecycle,在其所有的生命週期方法中列印一句話:
public class FragmentLiferecycle extends Fragment{@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);System.out.println("onActivityCreated");}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);System.out.println("onAttach");}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);System.out.println("onCreate");}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {System.out.println("onCreateView");return inflater.inflate(R.layout.fragment1, container, false);}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("onDestroy");}@Overridepublic void onDestroyView() {super.onDestroyView();System.out.println("onDestroyView");}@Overridepublic void onDetach() {super.onDetach();System.out.println("onDetach");}@Overridepublic void onPause() {super.onPause();System.out.println("onPause");}@Overridepublic void onResume() {super.onResume();System.out.println("onResume");}@Overridepublic void onStart() {super.onStart();System.out.println("onStart");}@Overridepublic void onStop() {super.onStop();System.out.println("onStop");}} 定義Activity布局檔案,用於顯示該Fragment,布局很簡單,一個按鈕,一個Fragment顯示容器activity_fragment_liferecycle.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:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="0px" android:layout_weight="1" android:text="顯示Fragment" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="wrap_content" android:layout_height="0px" android:layout_weight="7" /></LinearLayout>
定義Activity類FragmentLiferecycleActivity
public class FragmentLiferecycleActivity extends FragmentActivity {public Button mButton;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.activity_fragment_liferecycle);mButton = (Button)findViewById(R.id.button);mButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {FragmentLiferecycle fragment = new FragmentLiferecycle();getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();}});}}
點擊"顯示Fragment"按鈕,Logcat輸出:
onAttach
onCreate
onCreateView
onActivityCreated
onStart
onResume
此時點擊Home鍵,增加輸出:
onPause
onStop
發現只是暫停了該Fragment,並沒有銷毀,這點和Activity類似;
再次點擊程式表徵圖進入介面,增加輸出:
onStart
onResume
可以看到調用onStart恢複了之前的Fragment
按下返回鍵,增加輸出:
onPause
onStop
onDestroyView
onDestroy
onDetach
可以看到此時Fragment已經被銷毀,並且解除了和Activity的綁定;
android-----Fragment生命週期