利用Android的Log 演示一个activity的生命周期代码

来源:未知 责任编辑:责任编辑 发表时间:2013-08-22 04:52 点击:

代码: 
//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"); 
    } 
 
     

代码:
//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");
    }

   
}


演示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

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Android 完全退出程序
    • 原创:Android应用开发-Andorid歌词秀,含源码
    • android 屏幕保护
    • Android手机软件汉化教程---第四课 dex文件汉化
    • 众多Android 开源项目推荐,给力工作给力学习
    • Android Audio代码分析4
    • Android得到已安装的应用程序信息!
    • Android开发者指南(29) —— USB Host and Accessory
    • Android成长的幕后推手:工程师鲁宾
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1