[Android Training視頻系列]2.3 Stopping and Restarting an Activity

來源:互聯網
上載者:User

1.主要內容

本小節介紹onStop與onRestart以及onStart的使用,通過本講我們能學會應該在onStop與onStart裡面執行什麼樣的操作。
2.觀看視頻講解
http://www.eyeandroid.com/thread-11346-1-1.html

3.翻譯參考

 

停止與重啟Activity

 

在activity生命週期中,恰當的停止與重啟activity是很重要的,這樣能確保使用者感知到程式的存在並不會丟失他們的進度。在下面一些關鍵的情境中會涉及到停止與重啟:

   .使用者開啟“最近使用的程式(Recent Apps)”的菜單並從當前app切換到另外一個app,這個時候先前的app是被停止的。如果使用者通過“主畫面載入表徵圖(Home screen launcher icon”或“最近使用的程式(Recent Apps)”重新回到這個app,則activity會重啟。   .使用者在當前的app裡面執行啟動一個新的activity的操作時,當前activity會在第二個activity被建立後停止。如果使用者點擊back按鈕,之前的activtiy會被重啟。   .使用者在運行app時接受到一個來電通話。

Activity類提供了onStop()方法與onRestart()方法用於在activity停止與重啟時進行調用。和暫停狀態時部分阻塞使用者介面不同,停止狀態時UI不可見並且使用者的焦點轉移到另一個activity中。

|注意:因為系統在Activity停止時會在記憶體中儲存了Activity執行個體,所以很多時候不需要實現onStop()方法與onRestart()方法,甚至是onStart()方法。 因為大多數的Activity相對比較簡單,Activity會自動正常停止與重啟,只需要使用onPause()來停止正在啟動並執行動作並斷開系統資源連結。

 

1所示:當使用者離開Activity時系統會調用onStop()來停止Activity (1)。這個時候如果使用者返回,系統會調用onRestart()(2),緊接著會調用onStart()(3)與onResume()(4)。需要注意的是:無論什麼原因導致Activity停止,系統總是會在onStop()之前調用onPause()方法。

停止Activity

當activity調用onStop()方法時,該activity不再可見並且應該釋放所有不再需要的資源。一旦activity停止了,系統可能會摧毀activity的執行個體以回收記憶體,甚至,系統會不執行activity的onDestroy()回調方法而直接殺死你的app進程, 因此需要使用onStop()來釋放資源,從而避免記憶體流失。

儘管onPause()方法是在onStop()之前調用,通常應該使用onStop()來執行CPU密集型的關閉操作,例如把資料寫入資料庫。

例如,下面是一個在onStop()的方法裡面儲存筆記草稿到永久儲存介質的樣本:

@Overrideprotected void onStop() {    super.onStop();  // Always call the superclass method first    // Save the note's current draft, because the activity is stopping    // and we want to be sure the current note progress isn't lost.    ContentValues values = new ContentValues();    values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());    values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());    getContentResolver().update(            mUri,    // The URI for the note to update.            values,  // The map of column names and new values to apply to them.            null,    // No SELECT criteria are used.            null     // No WHERE columns are used.            );}

當Activity已經停止,Activity對象會儲存在記憶體中,並且在Activity恢複的時候被重新調用到。不需要在恢複到Resumed state狀態前重新初始化那些被儲存在記憶體中的組件。系統同樣儲存了每一個在布局中的視圖的目前狀態,如果使用者在EditText組件中輸入了文本,也會被儲存,因此不需要儲存與恢複它。

注意:即使系統會在Activity停止的時候銷毀這個Activity,系統仍然會儲存視圖對象(如文本編輯框中的文本)到一個Bundle中,並且在使用者返回這個Activity時恢複他們(下一節會介紹在Activity銷毀與重建時如何使用Bundle來儲存其他資料的狀態)。

啟動與重啟Activity

當Activity從Stopped狀態回到前台時會調用onRestart(),系統再調用onStart()方法,onStart()方法在每次Activity可見時都會被調用。onRestart()方法則是只在Activity從stopped狀態恢複時才會被調用,因此可以使用它來執行一些特殊的恢複工作,請注意之前是被stop而不是destrory。

使用onRestart()來恢複Activity狀態並不常見,因此對於這個方法如何使用沒有指導說明。但是,由於onStop()方法要做清除所有Activity資源的操作,在重新啟動Activtiy時需要重新執行個體化被清除的資源,同樣,在Activity第一次建立時要執行個體化那些資源。因為系統會在建立Activity與從停止狀態重啟Activity時都會調用onStart(),應該使用onStart()作為onStop()所對應的方法。

例如:因為使用者很可能在回到Activity之前需要過一段時間,所以onStart()方法是一個比較好的用來驗證某些必須的功能是否已經準備好的地方。

@Overrideprotected void onStart() {    super.onStart();  // Always call the superclass method first        // The activity is either being restarted or started for the first time    // so this is where we should make sure that GPS is enabled    LocationManager locationManager =             (LocationManager) getSystemService(Context.LOCATION_SERVICE);    boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);        if (!gpsEnabled) {        // Create a dialog here that requests the user to enable GPS, and use an intent        // with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action        // to take the user to the Settings screen to enable GPS when they click "OK"    }}@Overrideprotected void onRestart() {    super.onRestart();  // Always call the superclass method first        // Activity being restarted from stopped state    }

當系統Destory一個Activity,它會為Activity調用onDestroy()方法。由於會在onStop方法裡面做釋放資源的操作,而onDestory方法則是最後去清除那些可能導致記憶體流失的地方,因此需要確保那些線程都被Destroy並且所有的操作都被停止。

 
相關文章

聯繫我們

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