標籤:
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
android Activity生命週期的例子