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 
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
", 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 }
原文地址:https://www.cnblogs.com/alimjan/p/7112559.html