本文實例講述了android編程之基于log演示一個activity生命周期。分享給大家供大家參考,具體如下:
利用android的log 演示一個activity的生命周期
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
//demoactivity.java package uni.activity; /* @author octobershiner 2011 7 22 se.hit */ import android.app.activity; import android.os.bundle; import android.util.log; public class activitydemoactivity extends activity { /** called when the activity is first created. */ private static final string tag = "demo" ; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); log.d( "demo" , "this is a test string " ); } protected void onstart(){ super .onstart(); log.i(tag, "the activity state---->onstart" ); } protected void onrestart(){ super .onrestart(); log.i(tag, "the activity state---->onreatart" ); } protected void onresume(){ super .onresume(); log.i(tag, "the activity state---->onresume" ); } protected void onpause(){ super .onpause(); log.i(tag, "the activity state---->onpause" ); } protected void onstop(){ super .onstop(); log.i(tag, "the activity state---->onstop" ); } protected void ondestroy(){ super .ondestroy(); log.i(tag, "the activity state---->ondestroy" ); } } |
這是演示的結果
利用log展示activity的生命周期
注釋表示 中間執行的操作 為方便的觀察數據,可以在logcat窗口(沒有的話可以在window菜單中的show view中調出)的右側單擊加號創建一個過濾器,我的例子中過濾的是demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//開始運行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 |
另外過濾查看log的方法:
實例
沒有logcat窗口的朋友可以在window菜單中的show view中調出窗口
五個圓圈分別可以過濾五種不同的log
注意右邊的綠色加號,單擊可以自定義自己的過濾器,名字隨便起就好了
by log tag欄目中 選擇你要創建的過濾規則,比如你要過濾出所遇tag標記為“yourdemo”的log,就可以在里面輸入yourdemo了
希望本文所述對大家android程序設計有所幫助。