Activity生命週期包含7種狀態
1 onCreate Called when the activity is first created. 當 activity第一次被創建時被調用
2 onStart Called when the activity is becoming visible to the user. 當activity被用戶看到時調用
3 onResume Called when the activity will start interacting with the user.當用戶可以對界面進行操作時調用
4 onPause Called when the system is about to start resuming a previous activity 當用戶將要離開當前頁面時調用
5 onRestart Called after your activity has been stopped, prior to it being started again當activity被停止後,在被開啓前被調用
6 onStop Called when the activity is no longer visible to the user 當用戶看不到此activity界面時調用
7 onDestroy
其生命週期以activity棧的方式進行管理, 當app啓動時系統默認會加載配置好的第一個activity,這時會調用onCreate 、 onStart、onResume方法,同時第一個activity入棧,假設第一個activity界面包含一個按鈕,點擊調用第二個activity,那麼會調用第一個activity的onPause、onStop方法,同時調用第二個activity的onCreate 、 onStart、onResume方法,第二個 activity 入棧;也就是說棧頂元素一定是用戶看到的activity。當用戶點擊返回時,第二個activity會調用onPause、onStop、onDestroy,同時第一個activity調用onRestart、onStart、onResume,第二個activity出棧。
問題1 : 什麼時候會調用ondestroy方法
當用戶退出程序、系統資源不充足、用戶手動調用finish方法
問題 2: 當用戶正在操作某個app,突然有電話打進來時,會調用那些方法?
首先會調用onPause 、onStop 保存用戶操作的狀態、數據,通話結束後會恢復通話之前的狀態