本文實例講述了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"); } }
這是示範的結果
利用LOGactivity的生命週期
操作為方便的觀察數據,可以在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
注意右邊的
五個圓圈可以過濾五種不同的log注意右邊的綠色加號,點擊可以自訂自己的過濾器,名字隨便起就好了by Log Tag欄中選擇你要創建的過濾規則,例如你要過濾出所遇TAG標記為「yourDemo」的log,就可以在裡面輸入yourDemo了希望本文所述對大家Android程式設計有幫助。
更多Android程式設計之基於Log示範一個activity生命週期實例詳解相關文章請關注PHP中文網!