package com.example.yanlei.yl2; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnKeyListener; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends AppCompatActivity { public static final String TAG = "生命周期:"; String Str = ""; TextView pTextView = null; @Override public void onCreate(Bundle savedInstanceState) { //当创建此Activity的时候回调 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(TAG, "onCreate"); try { Str = Str + "创建:\n"; pTextView.setText(Str); } catch (Exception e) { //Log.e(TAG, "error : "+e.getMessage(), e); } } @Override protected void onDestroy() { //当销毁此Activity的时候回调 super.onDestroy(); Log.e(TAG, "onDestroy"); try { Str = Str + "摧毁:\n"; pTextView.setText(Str); } catch (Exception e) { //Log.e(TAG, "error : "+e.getMessage(), e); } } @Override protected void onPause() { //当暂停此Activity的时候回调 super.onPause(); Log.e(TAG, "onPause"); try { Str = Str + "暂停:\n"; pTextView.setText(Str); } catch (Exception e) { // Log.e(TAG, "error : "+e.getMessage(), e); } } @Override protected void onRestart() { //当重新开始此Activity的时候回调 super.onRestart(); Log.e(TAG, "onRestart"); try { Str = Str + "重新启动:\n"; pTextView.setText(Str); } catch (Exception e) { //Log.e(TAG, "error : "+e.getMessage(), e); } } @Override protected void onResume() { //当显示展示此Activity的界面的时候回调 super.onResume(); Log.e(TAG, "onResume"); try { Str = Str + "继续:\n"; pTextView.setText(Str); } catch (Exception e) { //Log.e(TAG, "error : "+e.getMessage(), e); } } @Override protected void onStart() { //当使用此Activity可以接受用户操作的时候回调 super.onStart(); Log.e(TAG, "onStart"); try { Str = Str + "开始:\n"; pTextView.setText(Str); } catch (Exception e) { //Log.e(TAG, "error : "+e.getMessage(), e); } } @Override protected void onStop() { //当停止此Activity的时候回调 super.onStop(); Log.e(TAG, "onStop"); try { Str = Str + "停止:\n"; pTextView.setText(Str); } catch (Exception e) { //Log.e(TAG, "error : "+e.getMessage(), e); } } }
日志如下:
onCreate
onStart
onResume
onPause
onStop
onDestroy
时间: 2024-10-17 09:43:32