Fragment的生命周期大致是这个样子的:
package com.example.fragment03;import com.example.fragment03.R;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FirstFragment extends Fragment{ public static final String TAG = FirstFragment.class.getName(); public void log(String name) { Log.d("Method", name+" Has Be Called."); } /** * Fragment练级到父Activity的时候调用,在这里可以获取到Activity的应用 */ @Override public void onAttach(Activity activity) { log("onAttach"); super.onAttach(activity); } /** * 创建Fragment初始化的时候调用 */ @Override public void onCreate(Bundle savedInstanceState) { log("onCreate"); super.onCreate(savedInstanceState); } /** * 在这里将UI初始化并返回 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { log("onCreateView"); View view = inflater.inflate(R.layout.fragment_first,container,false); return view; } /** * 只有当Fragment和Activity都被创建后,调用、 * 当Fragment需要调用Activity里面的东西的时候,需要在这个方法里(之后)调用。 */ @Override public void onActivityCreated(Bundle savedInstanceState) { log("onActivityCreated"); super.onActivityCreated(savedInstanceState); } @Override public void onStart() { log("onStart"); super.onStart(); } @Override public void onResume() { log("onResume"); super.onResume(); } @Override public void onPause() { log("onPause"); super.onPause(); } @Override public void onStop() { log("onStop"); super.onStop(); } @Override public void onDestroyView() { log("onDestroyView"); super.onDestroyView(); } @Override public void onDestroy() { log("onDestroy"); super.onDestroy(); } @Override public void onDetach() { log("onDetach"); super.onDetach(); } }
运行结果如下:
新打开时候:03-18 13:19:28.991: D/Method(2509): onAttach Has Be Called.03-18 13:19:28.991: D/Method(2509): onCreate Has Be Called.03-18 13:19:28.991: D/Method(2509): onCreateView Has Be Called.03-18 13:19:28.995: D/Method(2509): onActivityCreated Has Be Called.03-18 13:19:28.995: D/Method(2509): onStart Has Be Called.03-18 13:19:28.995: D/Method(2509): onResume Has Be Called.//此时Fragment处于活动状态//这时候我按下切换至别的Fragment,打印日志如下:03-18 13:20:33.603: D/Method(2509): onPause Has Be Called.03-18 13:20:33.603: D/Method(2509): onStop Has Be Called.03-18 13:20:33.603: D/Method(2509): onDestroyView Has Be Called.//这时候我再将Fragment切换回来,打印日志如下:03-18 13:21:26.019: D/Method(2509): onCreateView Has Be Called.03-18 13:21:26.019: D/Method(2509): onActivityCreated Has Be Called.03-18 13:21:26.019: D/Method(2509): onStart Has Be Called.03-18 13:21:26.019: D/Method(2509): onResume Has Be Called.//这时候我按下Home键,将应用挂至后台,打印日志如下03-18 13:22:21.899: D/Method(2509): onPause Has Be Called.03-18 13:22:22.447: D/Method(2509): onStop Has Be Called.//重新将应用切换至前台打印日志如下:03-18 13:23:13.235: D/Method(2509): onStart Has Be Called.03-18 13:23:13.235: D/Method(2509): onResume Has Be Called.//我按下返回键退出应用:03-18 13:25:41.923: D/Method(2866): onPause Has Be Called.03-18 13:25:42.539: D/Method(2866): onStop Has Be Called.03-18 13:25:42.539: D/Method(2866): onDestroyView Has Be Called.03-18 13:25:42.539: D/Method(2866): onDestroy Has Be Called.03-18 13:25:42.539: D/Method(2866): onDetach Has Be Called.