现在的位置: 首页 > 综合 > 正文

android activity 生命周期测试

2012年06月09日 ⁄ 综合 ⁄ 共 6977字 ⁄ 字号 评论关闭

android  activity 测试
测试结果 正常情况下

1 一个activity从创建到显示调用顺序 onCreate->onStart->onResume //onResume开始时也会被调用,写在onResume中应只有恢复显示和创建共有的代码
2 按返回键退出activity onPause->onStop->onDestroy
3 从应用管理中“强行停止” onPause->onStop //无Destroy
4 按返回退出后长按home键调出应用 或 点击应用重新进入 onCreate->onStrat->onResume
5 按主页键退出 然后长按home键调出应用 或 点击应用重新进 onStrat->onResume //按主页键挂后台 无需onCreate
6 finish()操作 onPause->onStop->onDestroy
7 menu,submenu,dialog显示并返回 无操作 //无onPause
8 切换到另一个activity 1.onPause->2.onCreate->2.onStart->2.onResume->1.onStop //居然是切换着来的
9 8步骤后 按返回键 2.onPause->1.onStart->1.onResume->2.onStop->2.onDestroy //疑问:什么时候单独用onResume?
10 9步骤后 切换到同一个activity 1.onPause->2.onCreate->2.onStart->2.onResume->1.onStop
11 10步骤后 按主页键返回 2.onPause->2.onStop
12 11步骤后 重新点击应用进入 2.onStart->2.onResume
13 应用中来电 onPause->onStop //同activity切换
14 13步骤 后挂断返回应用 onStart->onResume //同activity切换
15 当activity2 设置 android:theme="@android:style/Theme.Dialog" 切换到activity2显示 1.onPause->2.onCreate->2.onStart->2.onResume->2.onStop->2.onDestroy
16 15步骤后 按返回键返回 2.onPause->1.onResume //应该注意一下 Destroy后才调用的pause.

测试结论

1 activity从创建到显示 onCreate->onStart->onResume
2 activity finish或者是按返回键 使其不显示 onPause->onStop->onDestroy //重新进入需要onCreate->onStart->onResume
3 activity 按主页键不显示 onPause->onStop //重新进入只需要onStart->onResume
4 activity间切换 1.onPause->2.onCreate->2.onStart->2.onResume->1.onStop
5 4步骤后按返回键返回前一个activity 2.onPause->1.onStart->1.onResume->2.onStop->onDestroy //下次再intent进入 得onCreate
6 menu dialog 无任何操作 //有些说的不可交互时调用onPause 奇怪 应该是我理解错了?
7 打开一个 android:theme="@android:style/Theme.Dialog" 的activity 1.onPause->2.onCreate->2.onStart->2.onResume->2.onStop->2.onDestroy ;注意:直接调用onStop onDestroy
8 7步骤后按返回键回到前一个activity 2.onPause->1.onResume//直接调用onPause 真乱 估计还有其他特殊情况

测试用代码
activity1.java

Java代码  收藏代码
  1. import android.app.Activity;  
  2. import android.app.AlertDialog;  
  3. import android.app.AlertDialog.Builder;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.Menu;  
  9. import android.view.SubMenu;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13.   
  14. public class activity1 extends Activity {  
  15.     /** Called when the activity is first created. */  
  16.     String a="activity_1";  
  17.     AlertDialog.Builder builder;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         Log.v(a,"onCreate");  
  23.         Button Btn_Destory=(Button)findViewById(R.id.Btn_Destory);  
  24.         Btn_Destory.setOnClickListener(new OnClickListener(){  
  25.   
  26.             @Override  
  27.             public void onClick(View arg0) {  
  28.                 // TODO Auto-generated method stub  
  29.                 finish();  
  30.             }  
  31.         });  
  32.           
  33.         builder =new Builder(this);  
  34.         builder.setTitle("title");  
  35.         builder.setMessage("message");  
  36.         builder.setPositiveButton("text"new DialogInterface.OnClickListener(){  
  37.   
  38.             @Override  
  39.             public void onClick(DialogInterface dialog, int id) {  
  40.                 // TODO Auto-generated method stub  
  41.                 dialog.dismiss();  
  42.             }  
  43.               
  44.         });  
  45.           
  46.           
  47.         Button Btn_Dialog=(Button)findViewById(R.id.Btn_Dialog);  
  48.         Btn_Dialog.setOnClickListener(new OnClickListener(){  
  49.             @Override  
  50.             public void onClick(View v) {  
  51.                 // TODO Auto-generated method stub  
  52.                 builder.show();  
  53.             }  
  54.         });  
  55.           
  56.         Button Btn_Intent=(Button)findViewById(R.id.Btn_Intent);  
  57.         Btn_Intent.setOnClickListener(new OnClickListener(){  
  58.   
  59.             @Override  
  60.             public void onClick(View view) {  
  61.                 // TODO Auto-generated method stub  
  62.                   
  63.                 startActivity2();  
  64.             }  
  65.               
  66.         });  
  67.           
  68.     }  
  69.     public void onStart(){  
  70.         Log.v(a,"onStart");  
  71.         super.onStart();  
  72.     }  
  73.     public void onResume(){  
  74.         Log.v(a,"onResume");  
  75.         super.onResume();  
  76.     }  
  77.     public void onPause(){  
  78.         Log.v(a,"onPause");  
  79.         super.onPause();  
  80.     }  
  81.     public void onStop(){  
  82.         Log.v(a,"onStop");  
  83.         super.onStop();  
  84.     }  
  85.     public void onDestroy(){  
  86.         Log.v(a,"onDestory");  
  87.         super.onDestroy();  
  88.     }  
  89.       
  90.     @Override  
  91.     public boolean onCreateOptionsMenu(Menu menu) {  
  92.         // TODO Auto-generated method stub  
  93.         menu.add(011"menu_1");  
  94.         menu.add(022"menu_2");  
  95.         SubMenu subMenu = menu.addSubMenu(1100100"submenu");  
  96.         subMenu.add(2101101"submenu_1");  
  97.         subMenu.add(2102102"submenu_2");  
  98.         return true;  
  99.     }  
  100.     public void startActivity2(){  
  101.         Intent intent=new Intent(activity1.this,activity_2.class);  
  102.         this.startActivity(intent);  
  103.     }  
  104. }  

main.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.     <Button   
  12.         android:id="@+id/Btn_Destory"  
  13.         android:text="finish"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         />  
  17.     <Button   
  18.         android:id="@+id/Btn_Dialog"  
  19.         android:text="Dialog"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         />  
  23.     <Button   
  24.         android:id="@+id/Btn_Intent"  
  25.         android:text="Intent"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.             />  
  29. </LinearLayout>  

activity2.java

Java代码  收藏代码
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.util.Log;  
  4.   
  5. public class activity_2 extends Activity{  
  6.     String a="activity_2";  
  7.     public void onCreate(Bundle savedInstanceState){  
  8.         super.onCreate(savedInstanceState);  
  9.         Log.v(a,"onCreate");  
  10.         setContentView(R.layout.main2);  
  11.     }  
  12.     public void onStart(){  
  13.         Log.v(a,"onStart");  
  14.         super.onStart();  
  15.     }  
  16.     public void onResume(){  
  17.         Log.v(a,"onResume");  
  18.         super.onResume();  
  19.     }  
  20.     public void onPause(){  
  21.         Log.v(a,"onPause");  
  22.         super.onPause();  
  23.     }  
  24.     public void onStop(){  
  25.         Log.v(a,"onStop");  
  26.         super.onStop();  
  27.     }  
  28.     public void onDestroy(){  
  29.         Log.v(a,"onDestory");  
  30.         super.onDestroy();  
  31.     }  
  32. }  

main2.xml

Xml代码  收藏代码
  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.     <TextView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="@string/hello" />  
  10. </LinearLayout>  

抱歉!评论已关闭.