Android Activity生命周期

jopen 12年前發布 | 2K 次閱讀 網絡開發

onCreate(創建) 、onStart(激活) 、onResume(恢復) 、onPause(暫停) 、onStop(停止) 、onDestroy(銷毀) 、onRestart(重啟)

Activity starts -->onCreate()-->onStart()-->onResume()-->activity is running--another activity comes in front of this activity-->onPause()--the activity is no longer visivble-->onStop()-->onDestroy()-->activity is shut down

notes:

        When the “onPause()“excute,other applications need memory ,then Process is killed(User navigates back to activity),last excute onCreate()。

 When  the “onPause()“excute,the activity comes to the foreground,so it will excute onRestart(),last excute onstart()。

各個函數使用的時間:

1、onCreate()    
Acitivity首次創建時被調用。用于設置Acitivity的布局文件,綁定按鈕監聽器等一些普通靜態操作。

2、onStart()
Acitivity對用戶可見時被調用。

3、onResume()
Acitivity獲得用戶焦點,即用戶可操作該Acitivity時被調用。

4、onPause()
應用程序啟動了其它Acitivity時被調用。一般用于保存當前Acitivity中的數據。

5、onStop()
Acitivity不可見時被調用。

6、onRestart()
已停止的Acitivity重新啟動時被調用。

7、onDestroy()
調用Acitivity的finish()方法或Android系統資源不足時被調用。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!