Fragment(碎片)是必须嵌入在 Activity(活动) 中使用的。Fragment的生命周期随着Activity的生命周期的变化而变化
一、首先让我们看下Activity和Fragment的生命周期对比
二、通过代码来看一下
1 package com.xqx_life; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.FragmentManager; 6 import android.app.FragmentTransaction; 7 import android.view.Menu; 8 9 public class MainActivity extends Activity { 10 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 MyFragment fragment = new MyFragment(); 17 FragmentManager manager = getFragmentManager(); 18 FragmentTransaction action = manager.beginTransaction(); 19 action.add(R.id.show_fragment,fragment ); 20 action.commit(); 21 22 System.out.println("现在执行Activity的 onCreate()方法"); 23 } 24 25 @Override 26 protected void onStart() { 27 // TODO Auto-generated method stub 28 super.onStart(); 29 System.out.println("现在执行Activity的 onStart()方法"); 30 } 31 32 @Override 33 protected void onResume() { 34 // TODO Auto-generated method stub 35 super.onResume(); 36 System.out.println("现在执行Activity的 onResume()方法"); 37 } 38 39 @Override 40 protected void onRestart() { 41 // TODO Auto-generated method stub 42 super.onRestart(); 43 System.out.println("现在执行Activity的 onRestart()方法"); 44 } 45 46 @Override 47 protected void onPause() { 48 // TODO Auto-generated method stub 49 super.onPause(); 50 System.out.println("现在执行Activity的 onPause()方法"); 51 } 52 53 @Override 54 protected void onStop() { 55 // TODO Auto-generated method stub 56 super.onStop(); 57 System.out.println("现在执行Activity的 onStop()方法"); 58 } 59 60 @Override 61 protected void onDestroy() { 62 // TODO Auto-generated method stub 63 super.onDestroy(); 64 System.out.println("现在执行Activity的 onDestroy()方法"); 65 } 66 67 68 69 }
1 package com.xqx_life; 2 3 import android.app.Activity; 4 import android.app.Fragment; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 public class MyFragment extends Fragment{ 11 @Override 12 public View onCreateView(LayoutInflater inflater, ViewGroup container, 13 Bundle savedInstanceState) { 14 // TODO Auto-generated method stub 15 System.out.println("现在执行Fragment的 onCreateView()方法"); 16 View view = inflater.inflate(R.layout.layout_fragment, container, false); 17 return view; 18 } 19 20 @Override 21 public void onAttach(Activity activity) { 22 // TODO Auto-generated method stub 23 System.out.println("现在执行Fragment的 onAttach()方法"); 24 super.onAttach(activity); 25 } 26 27 @Override 28 public void onCreate(Bundle savedInstanceState) { 29 // TODO Auto-generated method stub 30 System.out.println("现在执行Fragment的 onCreate()方法"); 31 super.onCreate(savedInstanceState); 32 } 33 34 @Override 35 public void onActivityCreated(Bundle savedInstanceState) { 36 // TODO Auto-generated method stub 37 System.out.println("现在执行Fragment的 onActivityCreated()方法"); 38 super.onActivityCreated(savedInstanceState); 39 } 40 41 @Override 42 public void onStart() { 43 // TODO Auto-generated method stub 44 System.out.println("现在执行Fragment的 onStart()方法"); 45 super.onStart(); 46 } 47 48 @Override 49 public void onResume() { 50 // TODO Auto-generated method stub 51 System.out.println("现在执行Fragment的 onResume()方法"); 52 super.onResume(); 53 } 54 55 @Override 56 public void onPause() { 57 // TODO Auto-generated method stub 58 System.out.println("现在执行Fragment的 onPause()方法"); 59 super.onPause(); 60 } 61 62 @Override 63 public void onStop() { 64 // TODO Auto-generated method stub 65 System.out.println("现在执行Fragment的 onStop()方法"); 66 super.onStop(); 67 } 68 69 @Override 70 public void onDestroyView() { 71 // TODO Auto-generated method stub 72 System.out.println("现在执行Fragment的 onDestroyView()方法"); 73 super.onDestroyView(); 74 } 75 76 @Override 77 public void onDestroy() { 78 // TODO Auto-generated method stub 79 System.out.println("现在执行Fragment的 onDestroy()方法"); 80 super.onDestroy(); 81 82 } 83 84 @Override 85 public void onDetach() { 86 // TODO Auto-generated method stub 87 System.out.println("现在执行Fragment的 onDetach()方法"); 88 super.onDetach(); 89 } 90 91 92 93 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 8 </LinearLayout>
注意清单文件最低SDK版本要设置为11
1 <uses-sdk 2 android:minSdkVersion="11" 3 android:targetSdkVersion="21" />
看一下效果:
启动activity之后的生命
关闭activity 结束之前的生命