【Based Android】利用Android的Log 演示一个activity的生命周期

 1 代码:  
2 //DemoActivity.java
3
4 package uni.activity;
5 /*
6 @author octobershiner
7 2011 7 22
8 SE.HIT
9 */
10 import android.app.Activity;
11 import android.os.Bundle;
12 import android.util.Log;
13
14 public class ActivityDemoActivity extends Activity {
15 /** Called when the activity is first created. */
16
17 private static final String TAG = "demo";
18 @Override
19 public void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 Log.d("demo", "this is a test string ");
23 }
24
25 protected void onStart(){
26 super.onStart();
27 Log.i(TAG, "The activity state---->onStart");
28 }
29
30 protected void onRestart(){
31 super.onRestart();
32 Log.i(TAG, "The activity state---->onReatart");
33 }
34
35 protected void onResume(){
36 super.onResume();
37 Log.i(TAG, "The activity state---->onResume");
38 }
39
40 protected void onPause(){
41 super.onPause();
42 Log.i(TAG, "The activity state---->onPause");
43 }
44
45 protected void onStop(){
46 super.onStop();
47 Log.i(TAG, "The activity state---->onStop");
48 }
49
50 protected void onDestroy(){
51 super.onDestroy();
52 Log.i(TAG, "The activity state---->onDestroy");
53 }
54
55
56 }

演示sundy留的小作业 截取LOG

这是演示的结果

//利用LOG展示activity的生命周期  
//注释表示 中间执行的操作 为方便的观察数据,可以在LOGCAT窗口(没有的话可以在window菜单中的show view中调出)的右侧单击加号创建一个过滤器,我的例子中过滤的是demo

//开始运行demo 
07-22 11:18:19.311: INFO/demo(281): The activity state---->onStart
07-22 11:18:19.311: INFO/demo(281): The activity state---->onResume

//按下了back键 返回 activity从stack中弹出
07-22 11:18:34.821: INFO/demo(281): The activity state---->onPause
07-22 11:18:35.090: INFO/demo(281): The activity state---->onStop
07-22 11:18:35.090: INFO/demo(281): The activity state---->onDestroy

//再次启动demo
07-22 11:18:45.550: INFO/demo(281): The activity state---->onStart
07-22 11:18:45.550: INFO/demo(281): The activity state---->onResume

//按下了HOME键 当前TASK 处于后台转态,系统保存状态
07-22 11:18:53.750: INFO/demo(281): The activity state---->onPause
07-22 11:18:54.820: INFO/demo(281): The activity state---->onStop

//再次启动demo 回复原来的TASK activity在栈顶
07-22 11:19:03.550: INFO/demo(281): The activity state---->onReatart
07-22 11:19:03.550: INFO/demo(281): The activity state---->onStart
07-22 11:19:03.550: INFO/demo(281): The activity state---->onResume

posted @ 2011-10-22 19:46  leeon  阅读(1381)  评论(0编辑  收藏  举报