標籤:應用程式 介面 安卓 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()