Android中activity的生命週期以及使用情境

來源:互聯網
上載者:User

標籤:應用程式   介面   安卓   android   遊戲   

#1完整生命週期代碼如下。
package com.wzw.lifecycle;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {//被建立的時候調用的方法 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("oncreate");}//被銷毀的時候調用的方法@Overrideprotected void onDestroy() {System.out.println("ondestory");super.onDestroy();}//當activity介面使用者可見的時候調用的方法@Overrideprotected void onStart() {System.out.println("onstart");super.onStart();}@Overrideprotected void onRestart() {System.out.println("onrestart");super.onRestart();}//當activity介面使用者不可見的時候調用的方法@Overrideprotected void onStop() {System.out.println("onstop");super.onStop();}//介面開始擷取到焦點對應的方法。 (介面按鈕可以被點擊,文字框可以輸入內容)@Overrideprotected void onResume() {System.out.println("onresume");super.onResume();}//介面失去焦點對應的方法(暫停)(按鈕不可被點擊,文字框不可輸入內容,但是介面使用者仍然能看見)@Overrideprotected void onPause() {System.out.println("onpause");super.onPause();}public void click(View view){Intent intent = new Intent(this,SecondActivity.class);startActivity(intent);}}


#2生命週期

完整生命週期  oncreate--》onstart--》onresume--》onpause--》onstop--》ondestory


可視生命週期  onstart--》onresume--》onpause--》onstop


前台生命週期  onresume--》onpause  介面使用者仍然可見,但是失去焦點

#3使用情境1.應用程式退出自動儲存資料   ondestory   oncreate
2.應用程式最小化 暫停操作  onstop onstart  視頻播放器
3.遊戲的暫停和開始 前台生命週期
#4Tips建立時調用oncreate()  ----銷毀時調用ondestory()
當activity介面使用者可見的時候調用的方法onStart() ----當activity介面使用者不可見的時候調用的方法onStop() 

介面開始擷取到焦點對應的方法。 (介面按鈕可以被點擊,文字框可以輸入內容)onResume() ----介面失去焦點對應的方法(暫停)(按鈕不可被點擊,文字框不可輸入內容,但是介面使用者仍然能看見onPause()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.