finish()html
finishActivity(int requestCode) 強制關閉以前啓動的activityjava
Resumedandroid
The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)app
Pausedthis
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity
object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.spa
Stopped3d
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity
object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.rest
當activity處於pause和stop的狀態時,可能會被強制關閉,這時就須要保存activity的狀態,以便有更好的用戶體驗。code
onSaveInstanceState() onRestoreInstanceState()
只有在activity非正常退出(被回收或者強制關閉)時,onSaveInstanceState()纔會調用。orm
即便你沒有實現onSaveInstanceState()和onRestoreInstanceState()方法,activity的Layout上的控件(原生的)也是默認實現了這兩個方法。
除非你給控件指定了android:saveEnabled屬性爲false,或者調用setSaveEnable方法。另外,沒有指定id的控件,系統默認不保存狀態。
雖然控件會自動保存狀態,可是有時候咱們仍是須要保存一些成員變量,因此就必須實現activity的onSaveInstanceState()和onRestoreInstanceState()。
If the system callsonSaveInstanceState()
, it does so before onStop()
and possibly before onPause()
.
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language).
發生上面的變化時,系統會自動調用onDestory()和onCreate()方法。
The best way to handle such a restart is to save and restore the state of your activity usingonSaveInstanceState()
and onRestoreInstanceState()
(or onCreate()
), as discussed in the previous section.
在同一個進程中,activityA啓動activityB,A和B的生命週期是這樣的:
1.A的onPause()執行
2.B的onCreate(), onStart(), onResume()順序執行
3.A的onStop()執行,若是此時A不可見。