> 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文: [http://developer.android.com/training/basics/](http://developer.android.com/training/basics/activity-lifecycle/stopping.html)[activity](# "An activity represents a single screen with a user interface.")-lifecycle/stopping.html
恰當的停止與重啟我們的[activity](# "An activity represents a single screen with a user interface.")是很重要的,在[activity](# "An activity represents a single screen with a user interface.")生命周期中,他們能確保用戶感知到程序的存在并不會丟失他們的進度。在下面一些關鍵的場景中會涉及到停止與重啟:
- 用戶打開最近使用app的菜單并從我們的app切換到另外一個app,這個時候我們的app是被停止的。如果用戶通過手機主界面的啟動程序圖標或者最近使用程序的窗口回到我們的app,那么我們的[activity](# "An activity represents a single screen with a user interface.")會重啟。
- 用戶在我們的app里面執行啟動一個新[activity](# "An activity represents a single screen with a user interface.")的操作,當前[activity](# "An activity represents a single screen with a user interface.")會在第二個[activity](# "An activity represents a single screen with a user interface.")被創建后stop。如果用戶點擊back按鈕,第一個activtiy會被重啟。
- 用戶在使用我們的app時接收到一個來電通話.
[Activity](# "An activity represents a single screen with a user interface.")類提供了[onStop()](http://developer.android.com/reference/android/app/Activity.html#onStop())與[onRestart()](http://developer.android.com/reference/android/app/Activity.html#onRestart())方法來允許在[activity](# "An activity represents a single screen with a user interface.")停止與重啟時進行調用。不同于暫停狀態的部分阻塞UI,停止狀態是UI不再可見并且用戶的焦點轉移到另一個[activity](# "An activity represents a single screen with a user interface.")中.
> **Note:** 因為系統在[activity](# "An activity represents a single screen with a user interface.")停止時會在內存中保存[Activity](# "An activity represents a single screen with a user interface.")的實例,所以有時不需要實現onStop(),onRestart()甚至是onStart()方法. 因為大多數的[activity](# "An activity represents a single screen with a user interface.")相對比較簡單,[activity](# "An activity represents a single screen with a user interface.")會自己停止與重啟,我們只需要使用onPause()來停止正在運行的動作并斷開系統資源鏈接。

**Figure 1.** 上圖顯示:當用戶離開我們的[activity](# "An activity represents a single screen with a user interface.")時,系統會調用onStop()來停止[activity](# "An activity represents a single screen with a user interface.") (1). 這個時候如果用戶返回,系統會調用onRestart()(2), 之后會迅速調用onStart()(3)與onResume()(4). 請注意:無論什么原因導致[activity](# "An activity represents a single screen with a user interface.")停止,系統總是會在onStop()之前調用onPause()方法。
### 停止[activity](# "An activity represents a single screen with a user interface.")
當[activity](# "An activity represents a single screen with a user interface.")調用onStop()方法, [activity](# "An activity represents a single screen with a user interface.")不再可見,并且應該釋放那些不再需要的所有資源。一旦[activity](# "An activity represents a single screen with a user interface.")停止了,系統會在需要內存空間時摧毀它的實例(_和棧結構有關,通常back操作會導致前一個[activity](# "An activity represents a single screen with a user interface.")被銷毀_)。極端情況下,系統會直接殺死我們的app進程,并不執行[activity](# "An activity represents a single screen with a user interface.")的[onDestroy()](http://developer.android.com/reference/android/app/Activity.html#onDestroy())回調方法, 因此我們需要使用onStop()來釋放資源,從而避免內存泄漏。_(這點需要注意)_
盡管onPause()方法是在onStop()之前調用,我們應該使用onStop()來執行那些CPU intensive的shut-down操作,例如往數據庫寫信息。
例如,下面是一個在onStop()的方法里面保存筆記草稿到persistent storage的示例:
~~~
@Override
protected 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](# "An activity represents a single screen with a user interface.")已經停止后,[Activity](# "An activity represents a single screen with a user interface.")對象會保存在內存中,并在[activity](# "An activity represents a single screen with a user interface.") resume時被重新調用。我們不需要在恢復到Resumed state狀態前重新初始化那些被保存在內存中的組件。系統同樣保存了每一個在布局中的視圖的當前狀態,如果用戶在EditText組件中輸入了text,它會被保存,因此不需要保存與恢復它。
> **Note:** 即使系統會在[activity](# "An activity represents a single screen with a user interface.") stop時停止這個[activity](# "An activity represents a single screen with a user interface."),它仍然會保存[View](http://developer.android.com/reference/android/view/View.html)對象的狀態(比如[EditText](http://developer.android.com/reference/android/widget/EditText.html)中的文字) 到一個[Bundle](http://developer.android.com/reference/android/os/Bundle.html)中,并且在用戶返回這個[activity](# "An activity represents a single screen with a user interface.")時恢復它們(下一小節會介紹在[activity](# "An activity represents a single screen with a user interface.")銷毀與重新建立時如何使用[Bundle](http://developer.android.com/reference/android/os/Bundle.html)來保存其他數據的狀態).
### 啟動與重啟[activity](# "An activity represents a single screen with a user interface.")
當[activity](# "An activity represents a single screen with a user interface.")從Stopped狀態回到前臺時,它會調用onRestart().系統再調用onStart()方法,onStart()方法會在每次[activity](# "An activity represents a single screen with a user interface.")可見時都會被調用。onRestart()方法則是只在[activity](# "An activity represents a single screen with a user interface.")從stopped狀態恢復時才會被調用,因此我們可以使用它來執行一些特殊的恢復(restoration)工作,請注意之前是被stopped而不是destrory。
使用onRestart()來恢復[activity](# "An activity represents a single screen with a user interface.")狀態是不太常見的,因此對于這個方法如何使用沒有任何的guidelines。然而,因為onStop()方法應該做清除所有[activity](# "An activity represents a single screen with a user interface.")資源的操作,我們需要在重啟activtiy時重新實例化那些被清除的資源,同樣, 我們也需要在[activity](# "An activity represents a single screen with a user interface.")第一次創建時實例化那些資源。介于上面的原因,應該使用onStart()作為onStop()所對應方法。因為系統會在創建[activity](# "An activity represents a single screen with a user interface.")與從停止狀態重啟[activity](# "An activity represents a single screen with a user interface.")時都會調用onStart()。也就是說,我們在onStop里面做了哪些清除的操作,就該在onStart里面重新把那些清除掉的資源重新創建出來。
例如:因為用戶很可能在回到這個[activity](# "An activity represents a single screen with a user interface.")之前已經過了很長一段時間,所以onStart()方法是一個比較好的地方來驗證某些必須的系統特性是否可用。
~~~
@Override
protected 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"
}
}
@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first
// Activity being restarted from stopped state
}
~~~
當系統Destory我們的[activity](# "An activity represents a single screen with a user interface."),它會為[activity](# "An activity represents a single screen with a user interface.")調用onDestroy()方法。因為我們會在onStop方法里面做釋放資源的操作,那么onDestory方法則是我們最后去清除那些可能導致內存泄漏的地方。因此需要確保那些線程都被destroyed并且所有的操作都被停止。
- 序言
- Android入門基礎:從這里開始
- 建立第一個App
- 創建Android項目
- 執行Android程序
- 建立簡單的用戶界面
- 啟動其他的Activity
- 添加ActionBar
- 建立ActionBar
- 添加Action按鈕
- 自定義ActionBar的風格
- ActionBar的覆蓋層疊
- 兼容不同的設備
- 適配不同的語言
- 適配不同的屏幕
- 適配不同的系統版本
- 管理Activity的生命周期
- 啟動與銷毀Activity
- 暫停與恢復Activity
- 停止與重啟Activity
- 重新創建Activity
- 使用Fragment建立動態的UI
- 創建一個Fragment
- 建立靈活動態的UI
- Fragments之間的交互
- 數據保存
- 保存到Preference
- 保存到文件
- 保存到數據庫
- 與其他應用的交互
- Intent的發送
- 接收Activity返回的結果
- Intent過濾
- Android分享操作
- 分享簡單的數據
- 給其他App發送簡單的數據
- 接收從其他App返回的數據
- 給ActionBar增加分享功能
- 分享文件
- 建立文件分享
- 分享文件
- 請求分享一個文件
- 獲取文件信息
- 使用NFC分享文件
- 發送文件給其他設備
- 接收其他設備的文件
- Android多媒體
- 管理音頻播放
- 控制音量與音頻播放
- 管理音頻焦點
- 兼容音頻輸出設備
- 拍照
- 簡單的拍照
- 簡單的錄像
- 控制相機硬件
- 打印
- 打印照片
- 打印HTML文檔
- 打印自定義文檔
- Android圖像與動畫
- 高效顯示Bitmap
- 高效加載大圖
- 非UI線程處理Bitmap
- 緩存Bitmap
- 管理Bitmap的內存
- 在UI上顯示Bitmap
- 使用OpenGL ES顯示圖像
- 建立OpenGL ES的環境
- 定義Shapes
- 繪制Shapes
- 運用投影與相機視圖
- 添加移動
- 響應觸摸事件
- 添加動畫
- View間漸變
- 使用ViewPager實現屏幕側滑
- 展示卡片翻轉動畫
- 縮放View
- 布局變更動畫
- Android網絡連接與云服務
- 無線連接設備
- 使得網絡服務可發現
- 使用WiFi建立P2P連接
- 使用WiFi P2P服務
- 執行網絡操作
- 連接到網絡
- 管理網絡
- 解析XML數據
- 高效下載
- 為網絡訪問更加高效而優化下載
- 最小化更新操作的影響
- 避免下載多余的數據
- 根據網絡類型改變下載模式
- 云同步
- 使用備份API
- 使用Google Cloud Messaging
- 解決云同步的保存沖突
- 使用Sync Adapter傳輸數據
- 創建Stub授權器
- 創建Stub Content Provider
- 創建Sync Adpater
- 執行Sync Adpater
- 使用Volley執行網絡數據傳輸
- 發送簡單的網絡請求
- 建立請求隊列
- 創建標準的網絡請求
- 實現自定義的網絡請求
- Android聯系人與位置信息
- Android聯系人信息
- 獲取聯系人列表
- 獲取聯系人詳情
- 使用Intents修改聯系人信息
- 顯示聯系人頭像
- Android位置信息
- 獲取最后可知位置
- 獲取位置更新
- 顯示位置地址
- 創建和監視地理圍欄
- Android可穿戴應用
- 賦予Notification可穿戴特性
- 創建Notification
- 在Notifcation中接收語音輸入
- 為Notification添加顯示頁面
- 以Stack的方式顯示Notifications
- 創建可穿戴的應用
- 創建并運行可穿戴應用
- 創建自定義的布局
- 添加語音功能
- 打包可穿戴應用
- 通過藍牙進行調試
- 創建自定義的UI
- 定義Layouts
- 創建Cards
- 創建Lists
- 創建2D-Picker
- 創建確認界面
- 退出全屏的Activity
- 發送并同步數據
- 訪問可穿戴數據層
- 同步數據單元
- 傳輸資源
- 發送與接收消息
- 處理數據層的事件
- Android TV應用
- 創建TV應用
- 創建TV應用的第一步
- 處理TV硬件部分
- 創建TV的布局文件
- 創建TV的導航欄
- 創建TV播放應用
- 創建目錄瀏覽器
- 提供一個Card視圖
- 創建詳情頁
- 顯示正在播放卡片
- 幫助用戶在TV上探索內容
- TV上的推薦內容
- 使得TV App能夠被搜索
- 使用TV應用進行搜索
- 創建TV游戲應用
- 創建TV直播應用
- TV Apps Checklist
- Android企業級應用
- Ensuring Compatibility with Managed Profiles
- Implementing App Restrictions
- Building a Work Policy Controller
- Android交互設計
- 設計高效的導航
- 規劃屏幕界面與他們之間的關系
- 為多種大小的屏幕進行規劃
- 提供向下和橫向導航
- 提供向上和歷史導航
- 綜合:設計樣例 App
- 實現高效的導航
- 使用Tabs創建Swipe視圖
- 創建抽屜導航
- 提供向上的導航
- 提供向后的導航
- 實現向下的導航
- 通知提示用戶
- 建立Notification
- 當啟動Activity時保留導航
- 更新Notification
- 使用BigView風格
- 顯示Notification進度
- 增加搜索功能
- 建立搜索界面
- 保存并搜索數據
- 保持向下兼容
- 使得你的App內容可被Google搜索
- 為App內容開啟深度鏈接
- 為索引指定App內容
- Android界面設計
- 為多屏幕設計
- 兼容不同的屏幕大小
- 兼容不同的屏幕密度
- 實現可適應的UI
- 創建自定義View
- 創建自定義的View類
- 實現自定義View的繪制
- 使得View可交互
- 優化自定義View
- 創建向后兼容的UI
- 抽象新的APIs
- 代理至新的APIs
- 使用舊的APIs實現新API的效果
- 使用版本敏感的組件
- 實現輔助功能
- 開發輔助程序
- 開發輔助服務
- 管理系統UI
- 淡化系統Bar
- 隱藏系統Bar
- 隱藏導航Bar
- 全屏沉浸式應用
- 響應UI可見性的變化
- 創建使用Material Design的應用
- 開始使用Material Design
- 使用Material的主題
- 創建Lists與Cards
- 定義Shadows與Clipping視圖
- 使用Drawables
- 自定義動畫
- 維護兼容性
- Android用戶輸入
- 使用觸摸手勢
- 檢測常用的手勢
- 跟蹤手勢移動
- Scroll手勢動畫
- 處理多觸摸手勢
- 拖拽與縮放
- 管理ViewGroup中的觸摸事件
- 處理鍵盤輸入
- 指定輸入法類型
- 處理輸入法可見性
- 兼容鍵盤導航
- 處理按鍵動作
- 兼容游戲控制器
- 處理控制器輸入動作
- 支持不同的Android系統版本
- 支持多個控制器
- Android后臺任務
- 在IntentService中執行后臺任務
- 創建IntentService
- 發送工作任務到IntentService
- 報告后臺任務執行狀態
- 使用CursorLoader在后臺加載數據
- 使用CursorLoader執行查詢任務
- 處理查詢的結果
- 管理設備的喚醒狀態
- 保持設備的喚醒
- 制定重復定時的任務
- Android性能優化
- 管理應用的內存
- 代碼性能優化建議
- 提升Layout的性能
- 優化layout的層級
- 使用include標簽重用layouts
- 按需加載視圖
- 使得ListView滑動順暢
- 優化電池壽命
- 監測電量與充電狀態
- 判斷與監測Docking狀態
- 判斷與監測網絡連接狀態
- 根據需要操作Broadcast接受者
- 多線程操作
- 在一個線程中執行一段特定的代碼
- 為多線程創建線程池
- 啟動與停止線程池中的線程
- 與UI線程通信
- 避免出現程序無響應ANR
- JNI使用指南
- 優化多核處理器(SMP)下的Android程序
- Android安全與隱私
- Security Tips
- 使用HTTPS與SSL
- 為防止SSL漏洞而更新Security
- 使用設備管理條例增強安全性
- Android測試程序
- 測試你的Activity
- 建立測試環境
- 創建與執行測試用例
- 測試UI組件
- 創建單元測試
- 創建功能測試
- 術語表