大龙的博客

常用链接

统计

最新评论

Android Activity的生命周期

1. 启动Activity:onCreate()->onStart()->onResume()->Activity is running

2. 按back键返回:onPause()->onStop()->onDestroy() 再次启动时:onCreate()->onStart()->onResume()->Activity is running

3.按home键返回:onPause()->onStop() 再次启动时:onRestart()->onStart()->onResume()->Activity is running

4.切换到别的Activity(当前Activity不finish): onPause()->onStop() 再次启动时:onCreate()->onStart()->onResume()->Activity is running


5.切换到别的Activity(当前Activity finish): onPause()->onStop()->onDestroy() 再次启动时:onCreate()->onStart()->onResume()->Activity is running



package com.Aina.Android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Test_CeShi extends Activity {
	/** Called when the activity is first created. */
	private int i = 1;
	private Button btn;
	/**
	 * 创建
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i("TAG-onCreate", "onCreate()------------" + (i++));
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn = (Button) this.findViewById(R.id.Button01);
		btn.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Test_CeShi.this,fg.class);
				Test_CeShi.this.startActivity(intent);
//				Test_CeShi.this.finish();
			}
			
		});
	}

	/**
	 * 重新开始
	 */
	@Override
	protected void onRestart() {
		Log.i("TAG-onRestart", "onRestart()------------" + (i++));
		super.onRestart();
	}

	/**
	 * 启动
	 */
	@Override
	protected void onStart() {
		Log.i("TAG-onStart", "onStart()------------" + (i++));

		super.onStart();
	}

	/**
	 * 重新启动
	 */
	@Override
	protected void onResume() {
		Log.i("TAG-onResume", "onResume()------------" + (i++));
		super.onResume();
	}

	/**
	 * 暂停
	 */
	@Override
	protected void onPause() {
		Log.i("TAG-onPause", "onPause()------------" + (i++));

		super.onPause();
	}

	/**
	 * 停止
	 */
	@Override
	protected void onStop() {
		Log.i("TAG-onStop", "onStop()------------" + (i++));
		super.onStop();
	}

	/**
	 * 销毁
	 */
	@Override
	protected void onDestroy() {
		Log.i("TAG-onDestroy", "onDestroy()------------" + (i++));
		super.onDestroy();
	}

}

posted on 2011-01-16 15:21 大龙 阅读(393) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理