天天看點

GoogleDoc - 溫故而知新Activity生命周期方法

3.建立Activity一般人所不知道的地方

  1)Activity裡的各個生命周期的方法一般執行什麼代碼   

   在onCreate()方法裡,對于高版本的API,應判斷一下系統的的版本再決定是否執行。

<code>// Make sure we're running on Honeycomb or higher to use ActionBar APIs</code>

<code>    </code><code>if</code> <code>(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) {</code>

<code>        </code><code>// For the main activity, make sure the app icon in the action bar</code>

<code>        </code><code>// does not behave as a button</code>

<code>        </code><code>ActionBar actionBar = getActionBar();</code>

<code>        </code><code>actionBar.setHomeButtonEnabled(</code><code>false</code><code>);</code>

<code>    </code><code>}</code>

 As long as the activity is still partially visible but currently not the activity in focus, it       remains paused.(Activity可見,但是沒有獲得焦點就會進行pause狀态)

Stop animations or other ongoing actions that could consume CPU.(停止動畫)

Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).

Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.(釋放網絡資源)

上面的文字中着重強調了,如果想要快速的跳轉到下一個Activity,不要将一些重量級的操作放在onPause裡,而應該放在onStop方法中。由下圖也可以領會這個意思:

<a href="http://s3.51cto.com/wyfs02/M02/76/5B/wKioL1ZQlH6BUzbeAAA-mVJomWI765.png" target="_blank"></a>

一般初始化相機的方法寫在onResume()方法中,release camera的邏輯寫在onPause中。

  Google重點強調了onRestart與onStart的差別,強烈要求onStart()與onStop配對使用,因為onRestart隻有在從stop狀态傳回的時候被調用,在Activity建立的時候并不會被調用。

  2)儲存Activity中View的資訊

  Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).  (比如在切屏的時候,Activity會銷毀,但Activity元件資料會消除怎麼辦?)

  具體怎麼儲存View的狀态,View的狀态又會傳到哪兒去?

  在代碼裡儲存資訊

<code>@Override</code>

<code>protected</code> <code>void</code> <code>onCreate(Bundle savedInstanceState) {</code>

<code>    </code><code>super</code><code>.onCreate(savedInstanceState); </code><code>// Always call the superclass first</code>

<code>   </code> 

<code>    </code><code>// Check whether we're recreating a previously destroyed instance</code>

<code>    </code><code>if</code> <code>(savedInstanceState != </code><code>null</code><code>) {</code>

<code>        </code><code>// Restore value of members from saved state</code>

<code>        </code><code>mCurrentScore = savedInstanceState.getInt(STATE_SCORE);</code>

<code>        </code><code>mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);</code>

<code>    </code><code>} </code><code>else</code> <code>{</code>

<code>        </code><code>// Probably initialize members with default values for a new instance</code>

<code>    </code><code>...</code>

<code>}</code>

 取出資訊

<code>public</code> <code>void</code> <code>onRestoreInstanceState(Bundle savedInstanceState) {</code>

<code>    </code><code>// Always call the superclass so it can restore the view hierarchy</code>

<code>    </code><code>super</code><code>.onRestoreInstanceState(savedInstanceState);</code>

<code>    </code><code>// Restore state members from saved instance</code>

<code>    </code><code>mCurrentScore = savedInstanceState.getInt(STATE_SCORE);</code>

<code>    </code><code>mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);</code>

<code></code>

      本文轉自屠夫章哥  51CTO部落格,原文連結:http://blog.51cto.com/4259297/1715641,如需轉載請自行聯系原作者

繼續閱讀