1.activity生命周期
2.fragment的生命周期
3.对比图
4.测试代码
1 package com.goso.testapp; 2 3 import android.app.Activity; 4 import android.app.ListFragment; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.ArrayAdapter; 11 import android.widget.ListView; 12 13 /** 14 * Demonstration of using ListFragment to show a list of items 15 * from a canned array. 16 */ 17 public class FragmentListArray extends Activity { 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 Log.e("HJJ", "Activity &&&& onCreate..."); 23 // Create the list fragment and add it as our sole content. 24 if (getFragmentManager().findFragmentById(android.R.id.content) == null) { 25 ArrayListFragment list = new ArrayListFragment(); 26 getFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 27 } 28 } 29 30 @Override 31 protected void onStart() { 32 // TODO Auto-generated method stub 33 super.onStart(); 34 Log.e("HJJ", "Activity &&&& onStart..."); 35 } 36 37 @Override 38 protected void onResume() { 39 // TODO Auto-generated method stub 40 super.onResume(); 41 Log.e("HJJ", "Activity &&&& onResume..."); 42 } 43 44 @Override 45 protected void onStop() { 46 // TODO Auto-generated method stub 47 super.onStop(); 48 Log.e("HJJ", "Activity &&&& onStop..."); 49 } 50 51 @Override 52 protected void onPause() { 53 // TODO Auto-generated method stub 54 super.onPause(); 55 Log.e("HJJ", "Activity &&&& onPause..."); 56 } 57 58 @Override 59 protected void onDestroy() { 60 // TODO Auto-generated method stub 61 super.onDestroy(); 62 Log.e("HJJ", "Activity &&&& onDestroy..."); 63 } 64 65 public static class ArrayListFragment extends ListFragment { 66 67 @Override 68 public void onAttach(Activity activity) { 69 // TODO Auto-generated method stub 70 Log.e("HJJ", "ArrayListFragment **** onAttach..."); 71 super.onAttach(activity); 72 } 73 74 @Override 75 public void onCreate(Bundle savedInstanceState) { 76 // TODO Auto-generated method stub 77 Log.e("HJJ", "ArrayListFragment **** onCreate..."); 78 super.onCreate(savedInstanceState); 79 } 80 81 @Override 82 public View onCreateView(LayoutInflater inflater, ViewGroup container, 83 Bundle savedInstanceState) { 84 // TODO Auto-generated method stub 85 Log.e("HJJ", "ArrayListFragment **** onCreateView..."); 86 return super.onCreateView(inflater, container, savedInstanceState); 87 } 88 89 @Override 90 public void onActivityCreated(Bundle savedInstanceState) { 91 super.onActivityCreated(savedInstanceState); 92 Log.e("HJJ", "ArrayListFragment **** onActivityCreated..."); 93 String[] array = new String[]{"C++", "JAVA", "PYTHON"}; 94 setListAdapter(new ArrayAdapter<String>(getActivity(), 95 android.R.layout.simple_list_item_1, array)); 96 } 97 98 @Override 99 public void onStart() { 100 // TODO Auto-generated method stub 101 Log.e("HJJ", "ArrayListFragment **** onStart..."); 102 super.onStart(); 103 } 104 105 @Override 106 public void onResume() { 107 Log.e("HJJ", "ArrayListFragment **** onResume..."); 108 // TODO Auto-generated method stub 109 super.onResume(); 110 } 111 112 @Override 113 public void onPause() { 114 Log.e("HJJ", "ArrayListFragment **** onPause..."); 115 // TODO Auto-generated method stub 116 super.onPause(); 117 } 118 119 @Override 120 public void onStop() { 121 Log.e("HJJ", "ArrayListFragment **** onStop..."); 122 // TODO Auto-generated method stub 123 super.onStop(); 124 } 125 126 @Override 127 public void onDestroyView() { 128 Log.e("HJJ", "ArrayListFragment **** onDestroyView..."); 129 // TODO Auto-generated method stub 130 super.onDestroyView(); 131 } 132 133 @Override 134 public void onDestroy() { 135 // TODO Auto-generated method stub 136 Log.e("HJJ", "ArrayListFragment **** onDestroy..."); 137 super.onDestroy(); 138 } 139 140 @Override 141 public void onDetach() { 142 Log.e("HJJ", "ArrayListFragment **** onDetach..."); 143 // TODO Auto-generated method stub 144 super.onDetach(); 145 } 146 147 @Override 148 public void onListItemClick(ListView l, View v, int position, long id) { 149 Log.i("FragmentList", "Item clicked: " + id); 150 } 151 } 152 }
5.测试结果
onCreate过程 01-22 15:30:28.091: E/HJJ(10315): Activity &&&& onCreate... 01-22 15:30:28.091: E/HJJ(10315): ArrayListFragment **** onAttach... 01-22 15:30:28.091: E/HJJ(10315): ArrayListFragment **** onCreate... 01-22 15:30:28.115: E/HJJ(10315): ArrayListFragment **** onCreateView... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onActivityCreated... onStart过程 01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onStart... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onStart... onResume过程 01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onResume... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onResume... onPause过程 01-22 15:31:26.748: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:31:26.748: E/HJJ(10315): Activity &&&& onPause... onStop过程 01-22 15:31:27.638: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:31:27.638: E/HJJ(10315): Activity &&&& onStop... onStart过程 01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onStart... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onStart... onResume过程 01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onResume... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onResume... onPause过程 01-22 15:32:47.412: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:32:47.412: E/HJJ(10315): Activity &&&& onPause... onStop过程 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onStop... onDestroy过程 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDestroyView... 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDestroy... 01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onDetach... 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onDestroy...
搞定!!