Android 開發筆記___Activity的生命週期

來源:互聯網
上載者:User

標籤:pre   androi   set   com   tar   log   public   開始   入口   

一個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     @Override30     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     @Override38     protected void onStart() {39         refreshLife("onStart");40         super.onStart();41     }42 43     @Override44     protected void onStop() {45         refreshLife("onStop");46         super.onStop();47     }48 49     @Override50     protected void onResume() {51         refreshLife("onResume");52         super.onResume();53     }54 55     @Override56     protected void onPause() {57         refreshLife("onPause");58         super.onPause();59     }60 61     @Override62     protected void onRestart() {63         refreshLife("onRestart");64         super.onRestart();65     }66 67     @Override68     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的生命週期

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.