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.
<翻譯>在極端情況下,系統(tǒng)可能不調(diào)用最后一個(gè)生命周期函數(shù)onDestroy()就直接終止你的app進(jìn)程,所以在onStop中釋放可能會(huì)引起內(nèi)存泄露的資源是很重要的。
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.
<翻譯>盡管在調(diào)用onStop()方法之前會(huì)調(diào)用onPause()方法,你應(yīng)該在onStop()方法執(zhí)行像往數(shù)據(jù)庫(kù)里寫(xiě)數(shù)據(jù)這樣的耗時(shí),大計(jì)算量的停止操作(?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狀態(tài)所調(diào)用的生命周期函數(shù)中所創(chuàng)建的組件。
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.
<翻譯>系統(tǒng)同時(shí)也記錄布局中每個(gè)View中的當(dāng)前狀態(tài),如果用戶在EditText中輸入信息了,那這些信息會(huì)被保留下來(lái),所以你不必再保存和恢復(fù)它了。
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).
<翻譯>注意:就算系統(tǒng)在Activity處于Stopped狀態(tài)時(shí)銷(xiāo)毀了它,系統(tǒng)也會(huì)在Bundle(一個(gè)鍵-值對(duì))保存View的狀態(tài),并在用戶返回到同一個(gè)Activity實(shí)例中時(shí)恢復(fù)它們(下一節(jié)將著重討論使用Bundle來(lái)保存其他的狀態(tài)數(shù)據(jù)以防Activity被停止和重新創(chuàng)建)。
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()方法中再初始化。
總結(jié)
以上是生活随笔為你收集整理的Android---Activity 生命周期(三)Stopping Activity Restarting Activity的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 高级Drawable资源
- 下一篇: Fragment onCreateVie