创建项目
MainActivity.java
可以使用Android studio 自动生成代码
Code->Generate->Override Methods
选择(多选要按住Ctrl)
1 2 3 4 5 6 7
| onCreate onResume onPause onRestart onStart onStop onDestroy
|
添加日志信息
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 45 46 47 48 49 50 51 52
| package com.example.ex3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("MainActivity","onCreate"); } @Override protected void onResume() { super.onResume(); Log.e("MainActivity","onResume"); } @Override protected void onPause() { super.onPause(); Log.e("MainActivity","onPause"); } @Override protected void onRestart() { super.onRestart(); Log.e("MainActivity","onRestart"); } @Override protected void onStart() { super.onStart(); Log.e("MainActivity","onStart"); } @Override protected void onStop() { super.onStop(); Log.e("MainActivity","onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.e("MainActivity","onDestroy"); } }
|
设置日志过滤器
事件发生时会打印相应的日志