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
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class activity1 extends Activity {
/** Called when the activity is first created. */
String a="activity_1";
AlertDialog.Builder builder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(a,"onCreate");
Button Btn_Destory=(Button)findViewById(R.id.Btn_Destory);
Btn_Destory.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
builder =new Builder(this);
builder.setTitle("title");
builder.setMessage("message");
builder.setPositiveButton("text", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button Btn_Dialog=(Button)findViewById(R.id.Btn_Dialog);
Btn_Dialog.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
builder.show();
}
});
Button Btn_Intent=(Button)findViewById(R.id.Btn_Intent);
Btn_Intent.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
startActivity2();
}
});
}
public void onStart(){
Log.v(a,"onStart");
super.onStart();
}
public void onResume(){
Log.v(a,"onResume");
super.onResume();
}
public void onPause(){
Log.v(a,"onPause");
super.onPause();
}
public void onStop(){
Log.v(a,"onStop");
super.onStop();
}
public void onDestroy(){
Log.v(a,"onDestory");
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "menu_1");
menu.add(0, 2, 2, "menu_2");
SubMenu subMenu = menu.addSubMenu(1, 100, 100, "submenu");
subMenu.add(2, 101, 101, "submenu_1");
subMenu.add(2, 102, 102, "submenu_2");
return true;
}
public void startActivity2(){
Intent intent=new Intent(activity1.this,activity_2.class);
this.startActivity(intent);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/Btn_Destory"
android:text="finish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/Btn_Dialog"
android:text="Dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/Btn_Intent"
android:text="Intent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
activity2.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class activity_2 extends Activity{
String a="activity_2";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.v(a,"onCreate");
setContentView(R.layout.main2);
}
public void onStart(){
Log.v(a,"onStart");
super.onStart();
}
public void onResume(){
Log.v(a,"onResume");
super.onResume();
}
public void onPause(){
Log.v(a,"onPause");
super.onPause();
}
public void onStop(){
Log.v(a,"onStop");
super.onStop();
}
public void onDestroy(){
Log.v(a,"onDestory");
super.onDestroy();
}
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>