Fragment详解(二)--->生命周期详解

MainActivity如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment1;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. /** 
  10.  * Demo描述: 
  11.  * Fragment生命周期 
  12.  *  
  13.  * 测试方法: 
  14.  * 在界面中从上至下点击各个按钮 
  15.  *  
  16.  * 参考资料: 
  17.  * 1 Android疯狂讲义(第二版) 
  18.  * 2 http://blog.163.com/supered_yang@126/blog/static/4126004120131710545228/ 
  19.  * 3 http://blog.csdn.net/t12x3456/article/details/8104574 
  20.  *   Thank you very much 
  21.  *  
  22.  */  
  23. public class MainActivity extends Activity{  
  24.     private Button mStartActivityButton;  
  25.     private Button mAddFragmentButton;  
  26.     private Button mReplaceAndBackFragmentButton;  
  27.     private Button mReplaceFragmentButton;  
  28.     private Button mFinishButton;  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState){  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         init();  
  34.     }  
  35.       
  36.     private void init(){  
  37.         mStartActivityButton = (Button) findViewById(R.id.startActivityButton);  
  38.         mStartActivityButton.setOnClickListener(new ClickListenerImpl());  
  39.           
  40.         mAddFragmentButton = (Button) findViewById(R.id.addFragmentButton);  
  41.         mAddFragmentButton.setOnClickListener(new ClickListenerImpl());  
  42.           
  43.         mReplaceAndBackFragmentButton = (Button) findViewById(R.id.replaceAndBackFragmentButton);  
  44.         mReplaceAndBackFragmentButton.setOnClickListener(new ClickListenerImpl());  
  45.           
  46.         mReplaceFragmentButton = (Button) findViewById(R.id.replaceFragmentButton);  
  47.         mReplaceFragmentButton.setOnClickListener(new ClickListenerImpl());  
  48.           
  49.         mFinishButton = (Button) findViewById(R.id.finishButton);  
  50.         mFinishButton.setOnClickListener(new ClickListenerImpl());  
  51.     }  
  52.       
  53.     private class ClickListenerImpl implements OnClickListener{  
  54.         @Override  
  55.         public void onClick(View view) {  
  56.             switch (view.getId()) {  
  57.             case R.id.startActivityButton:  
  58.                 Intent intent = new Intent(MainActivity.this, DialogStyleActivity.class);  
  59.                 startActivity(intent);  
  60.                 break;  
  61.             case R.id.addFragmentButton:  
  62.                 TestLifecycleFragment testLifecycleFragment = new TestLifecycleFragment();  
  63.                 getFragmentManager()  
  64.                 .beginTransaction()  
  65.                 .add(R.id.linearLayoutContainer, testLifecycleFragment)  
  66.                 .commit();  
  67.                 break;  
  68.             case R.id.replaceAndBackFragmentButton:  
  69.                 AnotherFragment anotherFragment1 = new AnotherFragment();  
  70.                 getFragmentManager()  
  71.                 .beginTransaction()  
  72.                 .replace(R.id.linearLayoutContainer, anotherFragment1)  
  73.                 .addToBackStack("test")  
  74.                 .commit();  
  75.                 break;  
  76.             case R.id.replaceFragmentButton:  
  77.                 AnotherFragment anotherFragment2 = new AnotherFragment();  
  78.                 getFragmentManager()  
  79.                 .beginTransaction()  
  80.                 .replace(R.id.linearLayoutContainer, anotherFragment2)  
  81.                 .commit();  
  82.                 break;  
  83.             case R.id.finishButton:  
  84.                 finish();  
  85.                 break;  
  86.             default:  
  87.                 break;  
  88.             }  
  89.               
  90.         }  
  91.           
  92.     }  
  93. }  


TestLifecycleFragment如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment1;  
  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.Gravity;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.TextView;  
  12.   
  13. public class TestLifecycleFragment extends Fragment {  
  14.     final String TAG = "log";  
  15.     /** 
  16.      * 该Fragment被添加到Activity时调用. 
  17.      * 只会被调用一次 
  18.      */  
  19.     @Override  
  20.     public void onAttach(Activity activity) {  
  21.         super.onAttach(activity);  
  22.         Log.d(TAG, "-------onAttach------");  
  23.     }  
  24.   
  25.     /** 
  26.      * 创建该Fragment时调用. 
  27.      * 只会被调用一次 
  28.      */  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         Log.d(TAG, "-------onCreate------");  
  33.     }  
  34.   
  35.     /** 
  36.      * 每次创建和绘制该Fragment的View组件时调用. 
  37.      * Fragment会显示该方法返回的View 
  38.      */  
  39.     @Override  
  40.     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle data) {  
  41.         Log.d(TAG, "-------onCreateView------");  
  42.         TextView tv = new TextView(getActivity());  
  43.         tv.setGravity(Gravity.CENTER_HORIZONTAL);  
  44.         tv.setText("这是一个用于测试的Fragment");  
  45.         tv.setTextSize(40);  
  46.         return tv;  
  47.     }  
  48.   
  49.     /** 
  50.      * 当Fragment所在的Activity被启动完成后 
  51.      * 调用该方法 
  52.      */  
  53.     @Override  
  54.     public void onActivityCreated(Bundle savedInstanceState) {  
  55.         super.onActivityCreated(savedInstanceState);  
  56.         Log.d(TAG, "-------onActivityCreated------");  
  57.     }  
  58.   
  59.     /** 
  60.      * 启动Fragment时候调用该方法 
  61.      */  
  62.     @Override  
  63.     public void onStart() {  
  64.         super.onStart();  
  65.         Log.d(TAG, "-------onStart------");  
  66.     }  
  67.   
  68.     /** 
  69.      * 恢复Fragment时候调用该方法. 
  70.      * onStart()方法后一定会调用该onResume()方法 
  71.      */  
  72.     @Override  
  73.     public void onResume() {  
  74.         super.onResume();  
  75.         Log.d(TAG, "-------onResume------");  
  76.     }  
  77.       
  78.     /** 
  79.      * 暂停Fragment时候调用该方法 
  80.      */  
  81.     @Override  
  82.     public void onPause() {  
  83.         super.onPause();  
  84.         Log.d(TAG, "-------onPause------");  
  85.     }  
  86.   
  87.     /** 
  88.      * 停止Fragment时候调用该方法 
  89.      */  
  90.     @Override  
  91.     public void onStop() {  
  92.         super.onStop();  
  93.         Log.d(TAG, "-------onStop------");  
  94.     }  
  95.   
  96.     /** 
  97.      * 销毁该Fragment所包含的View调用该方法 
  98.      */  
  99.     @Override  
  100.     public void onDestroyView() {  
  101.         super.onDestroyView();  
  102.         Log.d(TAG, "-------onDestroyView------");  
  103.     }  
  104.   
  105.     /** 
  106.      * 销毁该Fragment时调用该方法 
  107.      * 该方法只会被调用一次 
  108.      */  
  109.     @Override  
  110.     public void onDestroy() {  
  111.         super.onDestroy();  
  112.         Log.d(TAG, "-------onDestroy------");  
  113.     }  
  114.   
  115.     /** 
  116.      * 将该Fragment从Activity中被删除,替换时调用该方法 
  117.      * 在onDestroy()方法后一定会调用该onDetach()方法. 
  118.      * 该方法只会被调用一次 
  119.      */  
  120.     @Override  
  121.     public void onDetach() {  
  122.         super.onDetach();  
  123.         Log.d(TAG, "-------onDetach------");  
  124.     }  
  125. }  


AnotherFragment如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment1;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.TextView;  
  10.   
  11. public class AnotherFragment extends Fragment {  
  12.     @Override  
  13.     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle data) {  
  14.         TextView textView = new TextView(getActivity());  
  15.         textView.setGravity(Gravity.CENTER_HORIZONTAL);  
  16.         textView.setText("另外一个Fragment");  
  17.         textView.setTextSize(40);  
  18.         return textView;  
  19.     }  
  20. }  


DialogStyleActivity如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment1;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6. /** 
  7.  * 备注说明: 
  8.  * 该Activity是对话框风格的Activity 
  9.  * 所以需要在配置文件中设置: 
  10.  * android:theme="@android:style/Theme.Holo.Dialog" 
  11.  * 
  12.  */  
  13. public class DialogStyleActivity extends Activity{  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState){  
  16.         super.onCreate(savedInstanceState);  
  17.         TextView textView = new TextView(this);  
  18.         textView.setText("对话框风格的Activity");  
  19.         setContentView(textView);  
  20.     }  
  21. }  


main.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     <LinearLayout  
  8.         android:id="@+id/linearLayoutContainer"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="160dp" >  
  11.     </LinearLayout>  
  12.   
  13.     <Button  
  14.         android:id="@+id/addFragmentButton"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="加载目标Fragment" />  
  18.       
  19.     <Button  
  20.         android:id="@+id/startActivityButton"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="启动对话框风格的Activity" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/replaceAndBackFragmentButton"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="替换目标Fragment,并加入Back栈" />  
  30.   
  31.     <Button  
  32.         android:id="@+id/replaceFragmentButton"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="wrap_content"  
  35.         android:text="替换目标Fragment" />  
  36.   
  37.     <Button  
  38.         android:id="@+id/finishButton"  
  39.         android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="退出" />  
  42.   
  43. </LinearLayout>  


AndroidManifest.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     package="cc.testsimplefragment1"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0">  
  7.     <uses-sdk  
  8.         android:minSdkVersion="11"  
  9.         android:targetSdkVersion="17" />  
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name">  
  13.         <activity  
  14.             android:name=".MainActivity"  
  15.             android:label="@string/app_name">  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.           
  22.         <activity  
  23.             android:theme="@android:style/Theme.Holo.Dialog"  
  24.             android:name=".DialogStyleActivity"  
  25.             android:label="@string/app_name" />  
  26.           
  27.     </application>  
  28. </manifest>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值