直接上代码:
第一个类:TestActivity
package feb.hxy;
import feb.hxy.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("TestActivity.onCreate");
button = (Button) findViewById(R.id.anotherActivity);
button.setText(string.anotherActivity);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, AnothorAcftivity.class);
startActivity(intent);
TestActivity.this.finish();
}
});
}
public void onStart() {
System.out.println("TestActivity.onStart");
super.onStart();
}
public void onResume() {
System.out.println("TestActivity.onResume");
super.onResume();
}
public void onPause() {
System.out.println("TestActivity.onPause");
super.onPause();
}
public void onStop() {
System.out.println("TestActivity.onStop");
super.onStop();
}
public void onDestroy() {
System.out.println("TestActivity.onDestory");
super.onDestroy();
}
public void onRestart() {
System.out.println("TestActivity.onRestart");
super.onRestart();
}
}
第二个类:AnothorAcftivity
package feb.hxy;
import feb.hxy.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AnothorAcftivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("AnothorAcftivity.onCreate");
button = (Button) findViewById(R.id.anotherActivity);
button.setText(string.back);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(AnothorAcftivity.this, TestActivity.class);
startActivity(intent);
//AnothorAcftivity.this.finish();
}
});
}
public void onStart() {
System.out.println("AnothorAcftivity.onStart");
super.onStart();
}
public void onResume() {
System.out.println("AnothorAcftivity.onResume");
super.onResume();
}
public void onPause() {
System.out.println("AnothorAcftivity.onPause");
super.onPause();
}
public void onStop() {
System.out.println("AnothorAcftivity.onStop");
super.onStop();
}
public void onDestroy() {
System.out.println("AnothorAcftivity.onDestory");
super.onDestroy();
}
public void onRestart() {
System.out.println("AnothorAcftivity.onRestart");
super.onRestart();
}
}
在模拟器中运行,查看DDMS,首先看到如下内容:
点击程序中的按钮,如下:
同样也点击第二个Activity中的按钮组件,如下:
注意:并没有调用到AnotherActivity的onDestroy()方法,因为AnotherActivity中注释了:
//AnothorAcftivity.this.finish();
由此可见当调用finish()时会调用onDestroy()。此时销毁Activity。
在点击模拟器上的返回键,结果如下:
![](http://hi.csdn.net/attachment/201203/4/0_13308498176Rtf.gif)
此时调用了AnotherActivity的onRestart()方法。
去掉AnotherActivity中的注释,按照以上的执行步骤,运行结果如下:
完整代码下载:TestAndroid(注:本程序使用的是2.2的模拟器)