1. An initial study on the life cycle of Fragment
Because the Fragment must be embedded in the Activity, the life cycle of the Fragment is closely related to the Activity it is located in.
If the Activity is suspended, all Fragments are suspended; if the Activity is stopped, all Fragments in this Activity cannot be started; if the Activity is destroyed, then all Fragments in it will be destroyed.
However, when the Activity is active, the state of the Fragment can be independently controlled, such as adding or removing the Fragment.
When fragment transaction is performed in this way, fragment can be placed in the activity's back stack so that the user can perform the return operation.
When using Fragment, you need to inherit Fragment or subclasses of Fragment (DialogFragment, ListFragment, PreferenceFragment, WebViewFragment), so the code of Fragment looks similar to that of Activity.
Whenever a Fragment is created, first add the following three callback methods:
onCreate(): The system calls this method when creating a Fragment. Here, the relevant components should be initialized, and some things that need to be retained even when they are suspended or stopped.
onCreateView(): When the system calls this method when the Fragment's UI is drawn for the first time, the method will return a View, and if the Fragment does not provide the UI, it can also return null. Note that if inherited from ListFragment, the default implementation of onCreateView() will return a ListView, so you don't need to implement it yourself.
onPause(): When the user leaves the Fragment, the first method is called, and some changes need to be submitted, because the user is likely to not return.
There are two ways to load a Fragment into an Activity:
Method 1: Add Fragment to Activity's layout file
Method 2: Dynamically add Fragment to Activity's code (recommended)
The first method is simple but not flexible enough. Adding a Fragment to the layout file of the Activity is equivalent to binding the Fragment and its views to the Activity view, and the fragment view cannot be switched during the life cycle of the activity.
The second method is more complicated, but it is also the only way to control fragments at runtime (load, remove, replace).
2. Lifecycle control examples
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Demonstration of using ListFragment to show a list of items * from a canned array. */ public class FragmentListArray extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); ("HJJ", "Activity &&&& onCreate..."); // Create the list fragment and add it as our sole content. if (getFragmentManager().findFragmentById() == null) { ArrayListFragment list = new ArrayListFragment(); getFragmentManager().beginTransaction().add(, list).commit(); } } @Override protected void onStart() { // TODO Auto-generated method stub (); ("HJJ", "Activity &&&& onStart..."); } @Override protected void onResume() { // TODO Auto-generated method stub (); ("HJJ", "Activity &&&& onResume..."); } @Override protected void onStop() { // TODO Auto-generated method stub (); ("HJJ", "Activity &&&& onStop..."); } @Override protected void onPause() { // TODO Auto-generated method stub (); ("HJJ", "Activity &&&& onPause..."); } @Override protected void onDestroy() { // TODO Auto-generated method stub (); ("HJJ", "Activity &&&& onDestroy..."); } public static class ArrayListFragment extends ListFragment { @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub ("HJJ", "ArrayListFragment **** onAttach..."); (activity); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub ("HJJ", "ArrayListFragment **** onCreate..."); (savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub ("HJJ", "ArrayListFragment **** onCreateView..."); return (inflater, container, savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { (savedInstanceState); ("HJJ", "ArrayListFragment **** onActivityCreated..."); String[] array = new String[]{"C++", "JAVA", "PYTHON"}; setListAdapter(new ArrayAdapter<String>(getActivity(), .simple_list_item_1, array)); } @Override public void onStart() { // TODO Auto-generated method stub ("HJJ", "ArrayListFragment **** onStart..."); (); } @Override public void onResume() { ("HJJ", "ArrayListFragment **** onResume..."); // TODO Auto-generated method stub (); } @Override public void onPause() { ("HJJ", "ArrayListFragment **** onPause..."); // TODO Auto-generated method stub (); } @Override public void onStop() { ("HJJ", "ArrayListFragment **** onStop..."); // TODO Auto-generated method stub (); } @Override public void onDestroyView() { ("HJJ", "ArrayListFragment **** onDestroyView..."); // TODO Auto-generated method stub (); } @Override public void onDestroy() { // TODO Auto-generated method stub ("HJJ", "ArrayListFragment **** onDestroy..."); (); } @Override public void onDetach() { ("HJJ", "ArrayListFragment **** onDetach..."); // TODO Auto-generated method stub (); } @Override public void onListItemClick(ListView l, View v, int position, long id) { ("FragmentList", "Item clicked: " + id); } } }
result:
onCreate process
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 process
01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onStart... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onStart...
onResume process
01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onResume... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onResume...
onPause process
01-22 15:31:26.748: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:31:26.748: E/HJJ(10315): Activity &&&& onPause...
onStop process
01-22 15:31:27.638: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:31:27.638: E/HJJ(10315): Activity &&&& onStop...
onStart process
01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onStart... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onStart...
onResume process
01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onResume... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onResume...
onPause process
01-22 15:32:47.412: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:32:47.412: E/HJJ(10315): Activity &&&& onPause...
onStop process
01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onStop...
onDestroy process
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...