关于activity的生命周期1
下面是官方文檔上的一幅比較經(jīng)典的activity生命周期示意圖:
其中涉及到七個生命周期函數(shù),下面摘自官方文檔的介紹:
| Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by?onStart(). | No | onStart() | |
| ???? | onRestart() Called after your activity has been stopped, prior to it being started again. Always followed by?onStart() | No | onStart() |
| Called when the activity is becoming visible to the user. Followed by?onResume()?if the activity comes to the foreground, or?onStop()?if it becomes hidden. | No | onResume()or?onStop() | |
| ???? | onResume() Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by?onPause(). | No | onPause() |
| Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either?onResume()?if the activity returns back to the front, or?onStop()?if it becomes invisible to the user. | Yes | onResume()or onStop() | |
| Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed. Followed by either?onRestart()?if this activity is coming back to interact with the user, oronDestroy()?if this activity is going away. | Yes | onRestart()or onDestroy() | |
| The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called?finish()?on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the?isFinishing()?method. | Yes | nothing |
1.onCreate()
Activity首次被創(chuàng)建時被調(diào)用,用于設(shè)置activity的布局文件,綁定按鈕監(jiān)聽器等一些靜態(tài)操作
2.onStart()
Activity對用戶可見時被調(diào)用
3.onResume()
Activity獲得用戶焦點(diǎn),即用戶可以操作activity時被調(diào)用
4.onPause()
應(yīng)用程序啟動了其他activity時被調(diào)用。一般用于當(dāng)前activity中的數(shù)據(jù)
5.onStop()
activity不可見時被調(diào)用
6.onRestart()
已停止的activity重新啟動被調(diào)用
7.onDestroy()
調(diào)用activity的finish()方法或者android系統(tǒng)資源不足的時候被調(diào)用
我們通過簡單的實(shí)驗(yàn)來說明一下:
下面的實(shí)驗(yàn)是創(chuàng)建兩個activity,跳轉(zhuǎn)的過程中,通過activity生命周期函數(shù)來說明activity的生命過程。由于布局很簡單,下面只給出截圖:
activity_main.xml:
two.xml:
MainActivity:(下面通過重寫activity生命周期函數(shù),來打印各個過程)
點(diǎn)擊(此處)折疊或打開
? ? super.onRestart();
? ? System.out.println("first?activity -->restart");
? ?
? ? }
TwoActivity:
點(diǎn)擊(此處)折疊或打開
? ? super.onRestart();
? ? System.out.println("second activity -->restart");
? ?
? ? }
此時我們通過運(yùn)行程序,通過Logcat可以看出打印出的各個生命周期階段:
啟動后,日志輸出:(第一個activity經(jīng)歷了oncreate,onstart,onresume,獲得焦點(diǎn))
點(diǎn)擊按鈕然后進(jìn)入第二個activity:(第一個activity可以是可見,但是失去了焦點(diǎn),第二個activity獲得了焦點(diǎn))
此時點(diǎn)擊back鍵:(此時第二個activity會失去焦點(diǎn)。而第一個activity之前被stop,此時會onrestart,重新獲得焦點(diǎn)。從上面的activity生命圖解可以很清楚的看出整個過程。這方面涉及到activity和Task相關(guān)知識,task以棧的形式存在,先啟動的activity會先入棧,最后啟動的activity會放在棧頂。上面第二個activity進(jìn)棧的時候,此時完全遮蓋了第一個activity,所以導(dǎo)致了第一個activity被onstop,但是沒有被銷毀,這也是當(dāng)?shù)诙€activity出棧,失去焦點(diǎn)后,第一個activity restart,而沒有oncreate的原因)
來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/29876893/viewspace-1815730/,如需轉(zhuǎn)載,請注明出處,否則將追究法律責(zé)任。
轉(zhuǎn)載于:http://blog.itpub.net/29876893/viewspace-1815730/
總結(jié)
以上是生活随笔為你收集整理的关于activity的生命周期1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双月楼月饼荣获茂名在线月饼品鉴佳绩
- 下一篇: Latex输入分段函数