Android基础之Android的Activity周期

直接上代码:

第一个类: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值