android生命周期的例子

第四课,对android中Activity生命周期的讲解。
[code]
package com.myclover.life;

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

/**
* 测试Activity的生命周期
* @author myclover
* </br>
* 执行流程如下:
* 在应用在启动时先执行onCreate,在界面可见但是不可点击时执行onStart,在界面可见并可操作时执行onResume,
* 当界面再次不可点击时执行onPause,在界面不可见时执行onStop,如果调用了finish(),那么接着会执行onDestroy
*
* 该测试程序执行的结果为:
* execute first onCreate---->execute first onStart---->execute first onResume---->(跳转)
* execute first onPause---->execute second onCreate---->execute second onStart---->
* execute second onResume---->execute first onStop---->execute first onDestroy---->(返回)
* execute second onPause---->execute first onCreate---->execute first onStart---->
* execute first onResume----> execute second onStop---->execute second onDestroy
*/
public class LifeDemoActivity extends Activity {

private static final String TAG = "LifeDemoActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "execute first onCreate!");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button redirectBtn = (Button)findViewById(R.id.redirectBtn);
Button toThirdBtn = (Button)findViewById(R.id.toThirdBtn);
redirectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//弹出确认选择框
Dialog dialog = new AlertDialog.Builder(LifeDemoActivity.this)
//设置弹出框标题
.setTitle(R.string.title)
//设置弹出框提示信息
.setMessage(R.string.message)
//设置确定按钮
.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
//点击确定按钮执行的方法
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
//设置Activity的跳转
intent.setClass(LifeDemoActivity.this, SecondActivity.class);
//启动新Activity
LifeDemoActivity.this.startActivity(intent);
//销毁当前Activity
LifeDemoActivity.this.finish();
Log.i(TAG, "redirect to second activity!");
}
})
//设置取消按钮
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
//点击取消按钮执行的方法
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i(TAG, "click cancel !");
}
}).create();
dialog.show();
}
});

toThirdBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//设置Activity的跳转
intent.setClass(LifeDemoActivity.this, ThirdActivity.class);
//启动新Activity
LifeDemoActivity.this.startActivity(intent);
//销毁当前Activity
// LifeDemoActivity.this.finish();
Log.i(TAG, "redirect to third activity!");
}
});

}

@Override
protected void onStart() {
Log.i(TAG, "execute first onStart!");
super.onStart();
}

@Override
protected void onRestart() {
Log.i(TAG, "execute first onRestart!");
super.onRestart();
}

@Override
protected void onResume() {
Log.i(TAG, "execute first onResume!");
super.onResume();
}

@Override
protected void onPause() {
Log.i(TAG, "execute first onPause!");
super.onPause();
}

@Override
protected void onStop() {
Log.i(TAG, "execute first onStop!");
super.onStop();
}

@Override
protected void onDestroy() {
Log.i(TAG, "execute first onDestroy!");
super.onDestroy();
}

}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值