Home >Java >javaTutorial >Detailed explanation of Android programming based on Log demonstration of an activity life cycle instance
The example in this article describes Android programming that demonstrates an activity life cycle based on Log. Share it with everyone for your reference, the details are as follows:
Use Android Log to demonstrate the life cycle of an activity
Code:
//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"); } }
This is the result of the demonstration
Use LOG to display the life cycle of the activity
The annotation indicates that the operations performed in the middle are convenient observation data. You can click on the right side of the LOGCAT window (if not, you can call it up in the show view in the window menu). Click the plus sign to create a filter. In my example, the filter is 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
In addition, how to filter and view the log:
Example
Log.i("yourDemo","this is my log");
Friends who do not have a LOGCAT window can Call up the window in the show view in the window menu
The five circles can filter five different logs
Pay attention to the green plus sign on the right. Click to customize your own filter, just name it whatever you want.
Select the filter rule you want to create in the Log Tag column. For example, if you want to filter out the logs with the tag "yourDemo", You can enter yourDemo in it
I hope this article will be helpful to everyone in Android programming.
For more detailed explanations of Android programming based on Log demonstration of an activity life cycle instance, please pay attention to the PHP Chinese website!