Android Activity的生命周期

[color=darkred]
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

[/color]


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();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值