fragment作用:用于多屏显示,可以把activity里面的逻辑进行分离。3.0的时候就开始有的(平板),4.0被加载到手机中。
fragment也可以看成一个view,依附于activity之上,fragment的生命周期可以说是由activity来控制的。
fragment上面必须有view才能被显示。
fragment:在布局中必须指定id和全类名 如下:
1 <fragment 2 android:id="@+id/fragment" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 class="com.android.hzy.fragment.MyFragment" />
具体代码
布局代码:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <fragment 8 android:id="@+id/fragment" 9 android:layout_width="fill_parent" 10 android:layout_height="fill_parent" 11 class="com.android.hzy.fragment.MyFragment" /> 12 13 </LinearLayout>
fragment
1 package com.android.hzy.fragment; 2 3 import android.app.Activity; 4 import android.app.Fragment; 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.TextView; 11 12 public class MyFragment extends Fragment { 13 14 @Override 15 public void onAttach(Activity activity) { 16 // TODO Auto-generated method stub 17 super.onAttach(activity); 18 19 Log.i("i", " MyFragment onAttach() "); 20 } 21 22 @Override 23 public void onCreate(Bundle savedInstanceState) { 24 // TODO Auto-generated method stub 25 super.onCreate(savedInstanceState); 26 27 Log.i("i", " MyFragment onCreate() "); 28 } 29 30 @Override 31 public View onCreateView(LayoutInflater inflater, ViewGroup container, 32 Bundle savedInstanceState) { 33 // TODO Auto-generated method stub 34 Log.i("i", " MyFragment onCreateView() "); 35 36 TextView tv = new TextView(getActivity()); 37 tv.setText("我的fragment"); 38 39 return tv; 40 } 41 42 @Override 43 public void onActivityCreated(Bundle savedInstanceState) { 44 // TODO Auto-generated method stub 45 super.onActivityCreated(savedInstanceState); 46 Log.i("i", " MyFragment onActivityCreated() "); 47 } 48 49 @Override 50 public void onStart() { 51 // TODO Auto-generated method stub 52 super.onStart(); 53 Log.i("i", " MyFragment onStart() "); 54 } 55 56 @Override 57 public void onResume() { 58 // TODO Auto-generated method stub 59 super.onResume(); 60 Log.i("i", " MyFragment onResume() "); 61 } 62 63 @Override 64 public void onPause() { 65 // TODO Auto-generated method stub 66 super.onPause(); 67 Log.i("i", " MyFragment onPause() "); 68 } 69 70 @Override 71 public void onStop() { 72 // TODO Auto-generated method stub 73 super.onStop(); 74 Log.i("i", " MyFragment onStop() "); 75 } 76 77 @Override 78 public void onDestroyView() { 79 // TODO Auto-generated method stub 80 super.onDestroyView(); 81 Log.i("i", " MyFragment onDestroyView() "); 82 } 83 84 @Override 85 public void onDestroy() { 86 // TODO Auto-generated method stub 87 super.onDestroy(); 88 Log.i("i", " MyFragment onDestroy() "); 89 } 90 91 @Override 92 public void onDetach() { 93 // TODO Auto-generated method stub 94 super.onDetach(); 95 Log.i("i", " MyFragment onDetach() "); 96 } 97 98 }
MainActivity
1 package com.android.hzy.fragment; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.util.Log; 6 7 public class MainActivity extends Activity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 14 Log.i("i", " MainActivity onCreate() "); 15 } 16 17 @Override 18 protected void onStart() { 19 // TODO Auto-generated method stub 20 super.onStart(); 21 Log.i("i", " MainActivity onStart() "); 22 } 23 24 @Override 25 protected void onResume() { 26 // TODO Auto-generated method stub 27 super.onResume(); 28 Log.i("i", " MainActivity onResume() "); 29 } 30 31 @Override 32 protected void onPause() { 33 // TODO Auto-generated method stub 34 super.onPause(); 35 Log.i("i", " MainActivity onPause() "); 36 } 37 38 @Override 39 protected void onStop() { 40 // TODO Auto-generated method stub 41 super.onStop(); 42 Log.i("i", " MainActivity onStop() "); 43 } 44 45 @Override 46 protected void onRestart() { 47 // TODO Auto-generated method stub 48 super.onRestart(); 49 Log.i("i", " MainActivity onRestart() "); 50 51 } 52 53 @Override 54 protected void onDestroy() { 55 // TODO Auto-generated method stub 56 super.onDestroy(); 57 Log.i("i", " MainActivity onDestroy() "); 58 } 59 60 61 }
***********************************总结*******************************************
* 显示到前台:
* MyFragment onAttach() 粘贴到activity上
* MyFragment onCreate() fragment创建
* MyFragment onCreateView() fragment创建自己的视图
* MainActivity onCreate()
* MyFragment onActivityCreated() 可以处理fragment数据的初始化
* MainActivity onStart()
* MyFragment onStart()
* MainActivity onResume()
* MyFragment onResume()
* 按后退键
* MyFragment onPause()
* MainActivity onPause()
* MyFragment onStop()
* MainActivity onStop()
* MyFragment onDestoryView() 销毁掉自己的视图
* MyFragment onDestory()
* MyFragment onDetach() 解除和activity的关系
* MainActivity onDestory()