直接上代码:

第一个类: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. HTML、HTML5重难点
  2. 数据调度组件:基于Azkaban协调时序任务执
  3. 7个最佳CSS优化技巧,可缩短页面加载时间
  4. 软测经典面试题(二)
  5. HBase优化 | 合理的使用编码压缩
  6. HBase重磅 | ApsaraDB HBase数据存储与分
  7. 快速掌握 10 个 HTML5 必备技巧
  8. HBase解读 | 阿里云HBase SQL(Phoenix)服
  9. Python爬取小说
  10. dns查询2:ResolveDns