1. 程式人生 > >Android 開發筆記___Activity的生命周期

Android 開發筆記___Activity的生命周期

clas 停止 refresh imp roi 一個 extend public tco

一個activity就是一個頁面,入口函數是oncreate()。

  • onCreate:創建頁面,把頁面上各個元素加載到內存
  • onStart:開始頁面,把頁面顯示在屏幕
  • onResume:恢復頁面,讓頁面活動起來
  • onPause:暫停頁面
  • onStop:停止頁面
  • onDestroy:銷毀頁面
  • onRestart:重啟頁面
 1 package com.example.alimjan.hello_world;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.os.Bundle;
6 import android.support.v7.app.AppCompatActivity; 7 import android.util.Log; 8 import android.widget.TextView; 9 10 import com.example.alimjan.hello_world.Utils.DateUtil; 11 12 /** 13 * Created by alimjan on 7/3/2017. 14 */ 15 16 public class class_3_5_1 extends AppCompatActivity { 17 18 private
final static String TAG = "ActHomeActivity"; 19 private TextView tv_life; 20 private String mStr = ""; 21 private int mState = 0; 22 23 private void refreshLife(String desc) { 24 Log.d(TAG, desc); 25 mStr = String.format("%s%s %s %s\n", mStr, DateUtil.getCurDateStr(), TAG, desc);
26 tv_life.setText(mStr); 27 } 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.code_3_5_1); 33 tv_life = (TextView) findViewById(R.id.tv_life); 34 refreshLife("onCreate"); 35 } 36 37 @Override 38 protected void onStart() { 39 refreshLife("onStart"); 40 super.onStart(); 41 } 42 43 @Override 44 protected void onStop() { 45 refreshLife("onStop"); 46 super.onStop(); 47 } 48 49 @Override 50 protected void onResume() { 51 refreshLife("onResume"); 52 super.onResume(); 53 } 54 55 @Override 56 protected void onPause() { 57 refreshLife("onPause"); 58 super.onPause(); 59 } 60 61 @Override 62 protected void onRestart() { 63 refreshLife("onRestart"); 64 super.onRestart(); 65 } 66 67 @Override 68 protected void onDestroy() { 69 refreshLife("onDestroy"); 70 super.onDestroy(); 71 } 72 public static void startHome(Context mContext) { 73 Intent intent = new Intent(mContext, class_3_5_1.class); 74 mContext.startActivity(intent); 75 } 76 77 }

Android 開發筆記___Activity的生命周期