Android 开发笔记___Activity的生命周期

【Android 开发笔记___Activity的生命周期】丈夫欲遂平生志,一载寒窗一举汤。这篇文章主要讲述Android 开发笔记___Activity的生命周期相关的知识,希望能为你提供帮助。
一个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 18private final static String TAG = "ActHomeActivity"; 19private TextView tv_life; 20private String mStr = ""; 21private int mState = 0; 22 23private void refreshLife(String desc) { 24Log.d(TAG, desc); 25mStr = String.format("%s%s %s %s\n", mStr, DateUtil.getCurDateStr(), TAG, desc); 26tv_life.setText(mStr); 27} 28 29@Override 30protected void onCreate(Bundle savedInstanceState) { 31super.onCreate(savedInstanceState); 32setContentView(R.layout.code_3_5_1); 33tv_life = (TextView) findViewById(R.id.tv_life); 34refreshLife("onCreate"); 35} 36 37@Override 38protected void onStart() { 39refreshLife("onStart"); 40super.onStart(); 41} 42 43@Override 44protected void onStop() { 45refreshLife("onStop"); 46super.onStop(); 47} 48 49@Override 50protected void onResume() { 51refreshLife("onResume"); 52super.onResume(); 53} 54 55@Override 56protected void onPause() { 57refreshLife("onPause"); 58super.onPause(); 59} 60 61@Override 62protected void onRestart() { 63refreshLife("onRestart"); 64super.onRestart(); 65} 66 67@Override 68protected void onDestroy() { 69refreshLife("onDestroy"); 70super.onDestroy(); 71} 72public static void startHome(Context mContext) { 73Intent intent = new Intent(mContext, class_3_5_1.class); 74mContext.startActivity(intent); 75} 76 77 }

 

    推荐阅读