Android---Activity 生命周期(三)Stopping Activity Restarting Activity
<翻譯>Activity被停止(stopped)和重啟(restarted)的一些情形:
1.1》The user opens the Recent Apps window and switches from your app to another app. The activity in your app that's currently in the foreground is stopped. If the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts.
1.2》The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.
1.3》The user receives a phone call while using your app on his or her phone.
?
2》Stopping Activity && Restarting Activity
Figure 1.
?When the user leaves your activity, the system calls onStop() to stop the activity (1).
?If the user returns while the activity is stopped, the system calls onRestart() (2), quickly followed by onStart() (3) and onResume() (4).?
Notice that no matter what scenario causes the activity to stop, the system always calls onPause() before calling onStop().
When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory.?
In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.
<翻譯>在極端情況下,系統可能不調用最后一個生命周期函數onDestroy()就直接終止你的app進程,所以在onStop中釋放可能會引起內存泄露的資源是很重要的。
Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.
<翻譯>盡管在調用onStop()方法之前會調用onPause()方法,你應該在onStop()方法執行像往數據庫里寫數據這樣的耗時,大計算量的停止操作(?larger, more CPU intensive shut-down operations)。
When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes.?
You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.?
<翻譯>不必重新初始化那些直到Resumed狀態所調用的生命周期函數中所創建的組件。
The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.
<翻譯>系統同時也記錄布局中每個View中的當前狀態,如果用戶在EditText中輸入信息了,那這些信息會被保留下來,所以你不必再保存和恢復它了。
Note: Even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such as text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the activity (the next lesson talks more about using a Bundle to save other state data in case your activity is destroyed and recreated).
<翻譯>注意:就算系統在Activity處于Stopped狀態時銷毀了它,系統也會在Bundle(一個鍵-值對)保存View的狀態,并在用戶返回到同一個Activity實例中時恢復它們(下一節將著重討論使用Bundle來保存其他的狀態數據以防Activity被停止和重新創建)。
3》Start/Restart Your Activity
However, because your onStop() method should essentially clean up all your activity's resources, you'll need to re-instantiate them when the activity restarts. Yet, you also need to instantiate them when your activity is created for the first time (when there's no existing instance of the activity).?
For this reason, you should usually use the onStart() callback method as the counterpart to the onStop() method, because the system calls onStart() both when it creates your activity and when it restarts the activity from the stopped state.
ps:通常onStrart()方法和onStop()方法配套使用,即在onStop中釋放的資源,要在onStart()方法中再初始化。
總結
以上是生活随笔為你收集整理的Android---Activity 生命周期(三)Stopping Activity Restarting Activity的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 高级Drawable资源
- 下一篇: Android 4.0新增Space及G