本文实例讲述了android编程之基于log演示一个activity生命周期。分享给大家供大家参考,具体如下:
利用android的log 演示一个activity的生命周期
代码:
//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
//开始运行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的方法:
实例
log.i("yourdemo","this is my log");
没有logcat窗口的朋友可以在window菜单中的show view中调出窗口
五个圆圈分别可以过滤五种不同的log
注意右边的绿色加号,单击可以自定义自己的过滤器,名字随便起就好了
by log tag栏目中 选择你要创建的过滤规则,比如你要过滤出所遇tag标记为“yourdemo”的log,就可以在里面输入yourdemo了
希望本文所述对大家android程序设计有所帮助。
更多android编程之基于log演示一个activity生命周期实例详解。
