activity生命周期
一、简介
二、代码实例
com.fry.activityLifeCycle.MainActivity
1 package com.fry.activityLifeCycle; 2 3 4 5 6 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.os.Bundle;10 import android.util.Log;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 15 16 17 public class MainActivity extends Activity{18 private Button btn_openActivty;//创建一个button对象19 private String tag=MainActivity.class.getSimpleName(); 20 protected void onCreate(Bundle savedInstanceState) {21 super.onCreate(savedInstanceState);//父类操作22 setContentView(R.layout.activity_main);//引入名为activity_main的界面23 btn_openActivty=(Button) findViewById(R.id.btn_openActivity);//找id为btn_openActivity的button24 /*25 * activity被创建时执行26 */27 Log.d(tag, "onCreate");28 btn_openActivty.setOnClickListener(new OnClickListener() { //设置button点击监听29 30 @Override31 public void onClick(View v) { //onclick事件32 // TODO Auto-generated method stub33 Intent intent=new Intent();//初始化intent34 intent.setClass(MainActivity.this,Activity01.class);//连接35 startActivity(intent);//打开activity36 37 }38 });39 }40 /*41 * activity可见时执行42 */43 @Override44 protected void onStart() {45 // TODO Auto-generated method stub46 super.onStart();47 Log.d(tag, "onStart");48 }49 50 /*51 * activity交互时执行52 */53 @Override54 protected void onResume() {55 // TODO Auto-generated method stub56 super.onResume();57 Log.d(tag, "onResume");58 }59 /*60 * activity重新可见时执行61 */62 @Override63 protected void onRestart() {64 // TODO Auto-generated method stub65 super.onRestart();66 Log.d(tag, "onRestart"); 67 }68 /*69 * activity暂停时执行70 */71 @Override72 protected void onPause() {73 // TODO Auto-generated method stub74 super.onPause();75 Log.d(tag, "onPause");76 }77 /*78 * activity停止时执行79 */80 @Override81 protected void onStop() {82 // TODO Auto-generated method stub83 super.onStop();84 Log.d(tag, "onStop");85 }86 /*87 * activity销毁时执行88 */89 @Override90 protected void onDestroy() {91 // TODO Auto-generated method stub92 super.onDestroy();93 Log.d(tag, "onDestroy");94 }95 }
测试结果
结果说明
1、打开程序时:执行oncreate、onstart、onresume
2、被其它程序夺取焦点时:执行onpause、onstop
3、重新获得焦点时:执行onrestart、onstart、onresume
4、关闭程序时:执行onpause、onstop、ondestroy
三、代码实例
结果说明
1、打开程序时:执行oncreate、onstart、onresume
2、暂停状态:执行onpause
3、从暂停状态返回运行状态:执行onresume
4、停止状态:执行onpause、onstop
5、从停止状态返回运行状态:执行onrestart、onstart、onresume
6、断开状态:执行onpause、onstop、ondestroy
com.fry.activityLifeCycle_2.MainActivity
1 package com.fry.activityLifeCycle_2; 2 3 4 5 6 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.os.Bundle; 10 import android.util.Log; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 15 16 17 public class MainActivity extends Activity implements OnClickListener{ 18 private Button btn_pause;//创建一个button对象 19 private Button btn_stop; 20 private Button btn_offLine; 21 private String tag=MainActivity.class.getSimpleName(); 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState);//父类操作 24 setContentView(R.layout.activity_main);//引入名为activity_main的界面 25 btn_pause=(Button) findViewById(R.id.btn_pause);//找id为btn_pause的button 26 btn_stop=(Button) findViewById(R.id.btn_stop);//找id为btn_stop的button 27 btn_offLine=(Button) findViewById(R.id.btn_offLine); 28 btn_pause.setOnClickListener(this); 29 btn_stop.setOnClickListener(this); 30 btn_offLine.setOnClickListener(this); 31 /* 32 * activity被创建时执行 33 */ 34 Log.d(tag, "onCreate"); 35 36 37 } 38 /* 39 * activity可见时执行 40 */ 41 @Override 42 protected void onStart() { 43 // TODO Auto-generated method stub 44 super.onStart(); 45 Log.d(tag, "onStart"); 46 } 47 48 /* 49 * activity交互时执行 50 */ 51 @Override 52 protected void onResume() { 53 // TODO Auto-generated method stub 54 super.onResume(); 55 Log.d(tag, "onResume"); 56 } 57 /* 58 * activity重新可见时执行 59 */ 60 @Override 61 protected void onRestart() { 62 // TODO Auto-generated method stub 63 super.onRestart(); 64 Log.d(tag, "onRestart"); 65 } 66 /* 67 * activity暂停时执行 68 */ 69 @Override 70 protected void onPause() { 71 // TODO Auto-generated method stub 72 super.onPause(); 73 Log.d(tag, "onPause"); 74 } 75 /* 76 * activity停止时执行 77 */ 78 @Override 79 protected void onStop() { 80 // TODO Auto-generated method stub 81 super.onStop(); 82 Log.d(tag, "onStop"); 83 } 84 /* 85 * activity销毁时执行 86 */ 87 @Override 88 protected void onDestroy() { 89 // TODO Auto-generated method stub 90 super.onDestroy(); 91 Log.d(tag, "onDestroy"); 92 } 93 @Override 94 public void onClick(View v) { 95 // TODO Auto-generated method stub 96 switch (v.getId()) { 97 case R.id.btn_pause://暂停状态 98 Intent intent=new Intent(); 99 intent.setClass(this, Activity02.class);100 startActivity(intent);101 break;102 case R.id.btn_stop://停止状态103 Intent intent2=new Intent();104 intent2.setClass(this, Activity01.class);105 startActivity(intent2);106 break;107 case R.id.btn_offLine://断开状态108 finish();109 default:110 break;111 }112 }113 }
/activityLifeCycle_2/AndroidManifest.xml
15 6 9 10 15 18 19 23 2420 21 22 25 26 27 28
用来设置页面成透明状态,从而形成暂停状态