2012年5月20日 星期日

[AndroidDev] Managing the Activity Lifecycle

  • 如果 AndroidManifest.xml 中沒有任何 activity 有宣告 MAIN action 與 LAUNCHER category,此 app 的 launch icon 則不會出現在 app list 中。
  • onPause <-> onResume
  • onStop -> onRestart -> onStart -> onResume

onCreate()
  • 適合處理在整個 activity life 中只應處理一次的邏輯,ex: 初始化 class-scope variables。

onRestart()
  • 當 activity 是從 onStop() 被啟動時,系統才會呼叫此 method。所以若是有在 onStop() 中釋放資源,記得在此初始或設定。

onStart()
  • activity 已為可見狀態,但會很快的便進入 onResume()。

onResume()
  • 會停留到 resume 完 activity 到暫停前的狀態。

onPause()
  • activity 仍為半可視狀態,ex: 被 dialog 覆蓋。
  • 可表示 user 離開此 activity 而進入 onStop(),所以可執行:
    • Stop animations or other ongoing actions that could consume CPU.
    • Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
    • Release system resources.
      • broadcast receivers, handles to sensors (like GPS).
      • Any resources that may affect battery life while your activity is paused and the user does not need them.
  • 不需要在此儲存 user 所輸入的資料,因為其實此 Activity instance 還是被儲存在 memory 中的,所以當在 onResume(),元件的狀態會再被重新載入,除非這是項明確功能:
    • ex: email 會自動儲存為草槁。
  • 不適合在此中處理會耗費 CPU 的工作,ex: 寫入 DB,因為會拖慢顯現下一個 activity 的速度,所以這類型的工作更適合在 onStop() 中處理。
    • 要到 activity B 前,activity A 需先進入 onPause()。

onStop()
  • 保證 UI 不是可視的,user 已在使用另一個 activity 或 app。
  • 適合做耗費 CPU 的工作:
    • ex: 寫入 DB。
  • 不需要在此儲存 user 所輸入的資料,因為其實此 Activity instance 還是被儲存在 memory 中的,所以當 onResume(),元件的狀態會再被重新載入。
    • ex: If the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.
    • 即使 activity 被 destroy,其中的 View objects 仍會被儲存在 Bundle 中,並且在 user 回到此 activity(the same instance of the activity) 時被重新載入。
  • 以下情況使得 activity A 會被 stopped 與 restarted:
    • user 由 Recent Apps window 開啟另一個 app,activity A 會進入 onStop(),若 user 再由 Home launcher icon or Recent Apps window 開啟你的 app,那麼 activity A 會進入 onRestart()。
    • 在你的 app 中進入下一個 activity,activity A 會進入 onStop(),若 user 按下 Back 鍵,activity A 會進入 onRestart()。
    • user 在使用 app 時有來電。

onDestroy()
  • The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one:
    • when you call finish() from within the onCreate() method.
    • onCreate() 會直接進入 onDestroy()。

以下情況可能會使得你的 activity 被 destroyed:
  • The user presses the Back button.
  • Calling finish().
  • Activity is in onStop() 且已長時間沒有被使用而被系統回收。
  • Foreground activity 需要更多資源,因此關閉 background processes to recover memory。

若 activity 是被系統回收,那麼系統會記下它的狀態(is called the "instance state", stored in a Bundle object.),以在 user 想開啟此 activity 時能 restore 該狀態。

onSaveInstanceState()
  • 當系統要 destroy this activity,會呼叫此 method,並且傳入 Bundle object 來儲存 activity 資訊,所以你若有需要保存的資訊也可儲存在此 Bundle 中。
  • 然後此 activity 會被系統 recreate 並且傳入與 destroy 時相同的 Bundle object,因此可以在 onCreate() 中從 Bundle 中取出之前所儲存的資訊。
    • 但因為 activity 可以是完全新建立或是重新被建立的,也就是說傳入的 Bundle 不一定都是有值的,若是要使用,務必判斷是不是 null。
    • 或者與其在 onCreate() 中 restore 資訊,也可以選擇在 onRestoreInstanceState() 做。
      • 此 method 會在 onStart() 後被呼叫。
      • 只有在 Bundle != null 時會被呼叫。
  • The default implementation of this method saves information about the state of the activity's view hierarchy:
    • ex: he text in an EditTextwidget or the scroll position of a ListView. 
    • 所以在此 method 和 onRestoreInstanceState() 中記得呼叫 super.xxxx; 以儲存與重載 the state of the view hierarchy。


* Reference
- Starting an Activity | Android Developers
- Pausing and Resuming an Activity | Android Developers
- Stopping and Restarting an Activity | Android Developers
- Recreating an Activity | Android Developers

沒有留言:

張貼留言