Android cainiao Study Notes 27 ---- simple use of Fragment, androidfragment
1. FragmentLifecycle:
Simply create a new MyFragment inherited from Fragment, override each lifecycle callback method, and directly output information that identifies the function called.
Override the lifecycle callback methods of MainActivity and output the identity information.
MyFragment. java:
1 public class MyFragment extends Fragment { 2 3 @Override 4 5 public void onActivityCreated(Bundle savedInstanceState) { 6 7 // TODO Auto-generated method stub 8 9 super.onActivityCreated(savedInstanceState); 10 11 Log.i("FRAGMENT","onActivityCreated()"); 12 13 } 14 15 @Override 16 17 public View onCreateView(LayoutInflater inflater, ViewGroup container, 18 19 Bundle savedInstanceState) { 20 21 // TODO Auto-generated method stub 22 23 Log.i("FRAGMENT","onCreateView()"); 24 25 TextView textView = new TextView(getActivity()); 26 27 textView.setText("This is a fragment"); 28 29 return textView; 30 31 } 32 33 @Override 34 35 public void onDestroyView() { 36 37 // TODO Auto-generated method stub 38 39 super.onDestroyView(); 40 41 Log.i("FRAGMENT","onDestroyView()"); 42 43 } 44 45 @Override 46 47 public void onAttach(Activity activity) { 48 49 // TODO Auto-generated method stub 50 51 super.onAttach(activity); 52 53 Log.i("FRAGMENT","onAttach()"); 54 55 } 56 57 @Override 58 59 public void onCreate(Bundle savedInstanceState) { 60 61 // TODO Auto-generated method stub 62 63 super.onCreate(savedInstanceState); 64 65 Log.i("FRAGMENT","onCreate()"); 66 67 } 68 69 @Override 70 71 public void onStart() { 72 73 // TODO Auto-generated method stub 74 75 super.onStart(); 76 77 Log.i("FRAGMENT","onStart()"); 78 79 } 80 81 @Override 82 83 public void onResume() { 84 85 // TODO Auto-generated method stub 86 87 super.onResume(); 88 89 Log.i("FRAGMENT","onResume()"); 90 91 } 92 93 @Override 94 95 public void onPause() { 96 97 // TODO Auto-generated method stub 98 99 super.onPause();100 101 Log.i("FRAGMENT","onPause()");102 103 }104 105 @Override106 107 public void onStop() {108 109 // TODO Auto-generated method stub110 111 super.onStop();112 113 Log.i("FRAGMENT","onStop()");114 115 }116 117 @Override118 119 public void onDestroy() {120 121 // TODO Auto-generated method stub122 123 super.onDestroy();124 125 Log.i("FRAGMENT","onDestroy()");126 127 }128 129 @Override130 131 public void onDetach() {132 133 // TODO Auto-generated method stub134 135 super.onDetach();136 137 Log.i("FRAGMENT","onDetach()");138 139 }140 141 }
Note: A View object must be returned in the onCreateView () method.
MainActivity. java:
1 public class MainActivity extends ActionBarActivity { 2 3 @Override 4 5 protected void onCreate(Bundle savedInstanceState) { 6 7 super.onCreate(savedInstanceState); 8 9 setContentView(R.layout.activity_main);10 11 Log.i("ACTIVITY","onCreate()");12 13 }14 15 @Override16 17 protected void onStop() {18 19 // TODO Auto-generated method stub20 21 super.onStop();22 23 Log.i("ACTIVITY","onStop()");24 25 }26 27 @Override28 29 protected void onDestroy() {30 31 // TODO Auto-generated method stub32 33 super.onDestroy();34 35 Log.i("ACTIVITY","onDestroy()");36 37 }38 39 @Override40 41 protected void onPause() {42 43 // TODO Auto-generated method stub44 45 super.onPause();46 47 Log.i("ACTIVITY","onPause()");48 49 }50 51 @Override52 53 protected void onResume() {54 55 // TODO Auto-generated method stub56 57 super.onResume();58 59 Log.i("ACTIVITY","onResume()");60 61 }62 63 @Override64 65 protected void onStart() {66 67 // TODO Auto-generated method stub68 69 super.onStart();70 71 Log.i("ACTIVITY","onStart()");72 73 }74 75 @Override76 77 protected void onRestart() {78 79 // TODO Auto-generated method stub80 81 super.onRestart();82 83 Log.i("ACTIVITY","onRestart()");84 85 }86 87 }
Layout file:
1 <fragment2 3 android:id="@+id/frag"4 5 android:name="cn.csc.fragment.MyFragment"6 7 android:layout_width="wrap_content"8 9 android:layout_height="wrap_content"/>
Note: you must use the name attribute to specify the permission name of the newly created fragment.
Running result:
Press the BACK button of the simulator:
Dalvik is inserted in the middle for garbage collection, but the overall order has not changed:
The calling process of each callback function throughout the lifecycle is as follows:
Display to front-end:
Append MyFragment onAttach () to activity
MyFragment onCreate () fragment Creation
MyFragment onCreateView () fragment create your own view
MainActivity onCreate () activity Creation
MyFragment onActivityCreated ()
MainActivity onStart ()
MyFragment onStart ()
MainActivity onResume ()
MyFragment onResume ()
Press the back key
MyFragment onPause ()
MainActivity onPause ()
MyFragment onStop ()
MainActivity onStop ()
MyFragment onDestoryView () destroys its own view
MyFragment onDestory ()
MyFragment onDetach () Relationship between release and activity
MainActivity onDetory ()
2.A simple two-FragmetFor example, click one of the following changes:
1) Create a New MyFragment inherited from ListFragment:
Public class MyFragment extends ListFragment {@ Override public void onActivityCreated (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onActivityCreated (savedInstanceState); setListAdapter (new ArrayAdapter <String> (getActivity (), android. r. layout. simple_list_item_activated_1, android. r. id. text1, new String [] {"6:00", "6:30", "7:20", "8:00", "8:40"}) ;}@ Override public void onListItemClick (ListView l, view v, int position, long id) {// TODO Auto-generated method stub super. onListItemClick (l, v, position, id); // set it to single-choice mode, and click getListView (). setChoiceMode (ListView. CHOICE_MODE_SINGLE); getListView (). setItemChecked (position, true); FragmentManager fm = getFragmentManager (); FragmentTransaction ft = fm. beginTransaction (); Fragment fr = new TmpFragment (position); ft. replace (R. id. frame, fr); ft. commit ();}}
2) create TmpFragment inherited from Fragment:
1 public class TmpFragment extends Fragment {2 3 private int index; 4 5 private static String [] items = new String [] {"Get up and wash", "punch For A Walk ", "Have Breakfast", "", "punch"}; 6 7 public TmpFragment (int index) {8 9 super (); 10 11 this. index = index; 12 13} 14 15 @ Override16 17 public View onCreateView (LayoutInflater inflater, ViewGroup container, 18 19 Bundle savedInstanceState) {20 21 // TODO Auto-generated method stub22 23 TextView TV = new TextView (getActivity (); 24 25 TV. setText (items [index]); 26 27 return TV; 28 29} 30 31}
3) Layout file:
1 <fragment 2 3 android:id="@+id/frag" 4 5 android:name="cn.csc.fragment1.MyFragment" 6 7 android:layout_width="0dp" 8 9 android:layout_height="match_parent"10 11 android:layout_weight="1"12 13 />14 15 <FrameLayout16 17 android:id="@+id/frame"18 19 android:layout_width="0dp"20 21 android:layout_height="wrap_content"22 23 android:layout_weight="1">24 25 </FrameLayout>
4) running result:
The above is the simple learning of Fragment, and you should study it carefully when necessary ......