看過很多書籍資料,通常都有這么一句話?!眔nStart()用于在活動(dòng)從不可見變?yōu)榭梢姇r(shí),加載可見資源“ 。但加載如layout、View等可見資源等工作是在onCreate階段完成的啊,onStart到底能做什么呢?
所以這句話很不好理解。之后在網(wǎng)上看到一段話:
For example, you should unregister listeners for GPS, sensors, etc in onStop() and register again in onStart(). If you register it in onCreate() and unregister in onDestroy(), then GPS service will work always and it will drain battery.
我們?nèi)绻褂胓ps或則其他傳感器時(shí),應(yīng)該在onStart函數(shù)中注冊(cè)監(jiān)聽器,而在OnStop函數(shù)中注銷監(jiān)聽器。使得我們僅在活動(dòng)的可見生命周期中調(diào)用系統(tǒng)資源,節(jié)約能耗。
從上面我們能夠得出結(jié)論,當(dāng)我們需要在活動(dòng)的可見生命周期中使用系統(tǒng)資源時(shí),應(yīng)該在OnStart中注冊(cè)。