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 *
* 执行流程如下: * 在应用在启动时先执行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]