直接上代码:

第一个类: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。

在点击模拟器上的返回键,结果如下:


此时调用了AnotherActivity的onRestart()方法。

去掉AnotherActivity中的注释,按照以上的执行步骤,运行结果如下:


完整代码下载TestAndroid(注:本程序使用的是2.2的模拟器)

更多相关文章

  1. Android隐藏软件盘
  2. Delphi XE5 android 获取网络状态
  3. org.gradle.api.GradleException: Lint found fatal errors whil
  4. Android显示GIF动画完整示例(一)
  5. Android的NDK开发(1)————Android(安卓)JNI简介与调用流程
  6. android 调用系统图片浏览器并返回图片路径
  7. android webview 7.0 h5调用打电话解决方案
  8. Android(安卓)使用Camera2 调用摄像头显示预览与拍照
  9. Android(安卓)复用自制的控件(2)

随机推荐

  1. Android的Socket通信编程实例
  2. Android事件分发机制完全解析,带你从源码
  3. Android的事件简介
  4. Android(安卓)UI【android 仿微信、QQ聊
  5. Android中View和ViewGroup介绍
  6. 一个初级程序员要学会的东西
  7. [置顶] [Android基础]Android中ListView详
  8. Android(安卓)XmlPullParser工具解析
  9. spring for android
  10. Android系统启动过程