本文實(shí)例講述了android編程之基于log演示一個(gè)activity生命周期。分享給大家供大家參考,具體如下:
利用android的log 演示一個(gè)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" ); } } |
這是演示的結(jié)果
利用log展示activity的生命周期
注釋表示 中間執(zhí)行的操作 為方便的觀察數(shù)據(jù),可以在logcat窗口(沒有的話可以在window菜單中的show view中調(diào)出)的右側(cè)單擊加號創(chuàng)建一個(gè)過濾器,我的例子中過濾的是demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//開始運(yùn)行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鍵 當(dāng)前task 處于后臺轉(zhuǎn)態(tài),系統(tǒng)保存狀態(tài) 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 回復(fù)原來的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的方法:
實(shí)例
沒有l(wèi)ogcat窗口的朋友可以在window菜單中的show view中調(diào)出窗口
五個(gè)圓圈分別可以過濾五種不同的log
注意右邊的綠色加號,單擊可以自定義自己的過濾器,名字隨便起就好了
by log tag欄目中 選擇你要?jiǎng)?chuàng)建的過濾規(guī)則,比如你要過濾出所遇tag標(biāo)記為“yourdemo”的log,就可以在里面輸入yourdemo了
希望本文所述對大家android程序設(shè)計(jì)有所幫助。