android生命周期的例子
16lz
2021-01-23
第四课,对android中Activity生命周期的讲解。
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() {@Overridepublic 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() {//点击确定按钮执行的方法@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent = new Intent();//设置Activity的跳转intent.setClass(LifeDemoActivity.this, SecondActivity.class);//启动新ActivityLifeDemoActivity.this.startActivity(intent);//销毁当前ActivityLifeDemoActivity.this.finish();Log.i(TAG, "redirect to second activity!");}})//设置取消按钮.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {//点击取消按钮执行的方法@Overridepublic void onClick(DialogInterface dialog, int which) {Log.i(TAG, "click cancel !");}}).create();dialog.show();}}); toThirdBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();//设置Activity的跳转intent.setClass(LifeDemoActivity.this, ThirdActivity.class);//启动新ActivityLifeDemoActivity.this.startActivity(intent);//销毁当前Activity//LifeDemoActivity.this.finish();Log.i(TAG, "redirect to third activity!");}}); }@Overrideprotected void onStart() {Log.i(TAG, "execute first onStart!");super.onStart();}@Overrideprotected void onRestart() {Log.i(TAG, "execute first onRestart!");super.onRestart();}@Overrideprotected void onResume() {Log.i(TAG, "execute first onResume!");super.onResume();}@Overrideprotected void onPause() {Log.i(TAG, "execute first onPause!");super.onPause();}@Overrideprotected void onStop() {Log.i(TAG, "execute first onStop!");super.onStop();}@Overrideprotected void onDestroy() {Log.i(TAG, "execute first onDestroy!");super.onDestroy();}}
更多相关文章
- 进度条背景的改变,界面更美观
- android xml界面布局常用属性概括
- android studio 新建项目 界面一直停在 【“building ‘ 项目名
- Android输入法遮挡了输入框,使用android:fitsSystemWindows="true
- android带有文字的图片按钮的两种实现方式
- Android 四大核心组件之Activity[生命周期篇]
- Android必备:Android Activity的生命周期