android activity 测试

[b]android activity 测试[/b]
[b]测试结果 正常情况下 [/b]
[table]
|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.

[/table]
[b]测试结论[/b]
[table]
|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 真乱 估计还有其他特殊情况
[/table]
测试用代码
[b]activity1.java[/b]


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);
}
}

[b]main.xml[/b]

<?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>

[b]activity2.java[/b]

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();
}
}

[b]main2.xml[/b]

<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值