Android 开发笔记___Activity的生命周期

时间:2022-06-08 16:09:59

一个activity就是一个页面,入口函数是oncreate()。

  • onCreate:创建页面,把页面上各个元素加载到内存
  • onStart:开始页面,把页面显示在屏幕
  • onResume:恢复页面,让页面活动起来
  • onPause:暂停页面
  • onStop:停止页面
  • onDestroy:销毁页面
  • onRestart:重启页面
 package com.example.alimjan.hello_world;

 import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView; import com.example.alimjan.hello_world.Utils.DateUtil; /**
* Created by alimjan on 7/3/2017.
*/ public class class_3_5_1 extends AppCompatActivity { private final static String TAG = "ActHomeActivity";
private TextView tv_life;
private String mStr = "";
private int mState = 0; private void refreshLife(String desc) {
Log.d(TAG, desc);
mStr = String.format("%s%s %s %s\n", mStr, DateUtil.getCurDateStr(), TAG, desc);
tv_life.setText(mStr);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_5_1);
tv_life = (TextView) findViewById(R.id.tv_life);
refreshLife("onCreate");
} @Override
protected void onStart() {
refreshLife("onStart");
super.onStart();
} @Override
protected void onStop() {
refreshLife("onStop");
super.onStop();
} @Override
protected void onResume() {
refreshLife("onResume");
super.onResume();
} @Override
protected void onPause() {
refreshLife("onPause");
super.onPause();
} @Override
protected void onRestart() {
refreshLife("onRestart");
super.onRestart();
} @Override
protected void onDestroy() {
refreshLife("onDestroy");
super.onDestroy();
}
public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_5_1.class);
mContext.startActivity(intent);
} }