Android API Demos學習(2) - Save & Restore State

來源:互聯網
上載者:User

展示了Activity被中止時是怎麼樣儲存狀態的。先瞭解幾點知識:
本例通過onSaveInstanceState()來儲存UI狀態,onRestoreInstanceState()恢複UI狀態。這個是Activity預設實現的,不需要我們調用。
onSaveInstanceState()不是Activity生命週期的一部分,在使用者明確退出Activity的時候不會執行,比如按Back鍵。
關鍵代碼:
 
<EditText android:id="@+id/saved" 
        android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/green" 
        android:text="@string/initial_text" 
        android:freezesText="true"> 
        <requestFocus /> 
    </EditText> 
 
<EditText  
        android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/red" 
        android:text="@string/initial_text"> 
    </EditText> 

一個定義了id,一個沒定義,Activity預設只對定義了id的元素儲存和恢複狀態。
android:freezesText為true是為了儲存當前常值內容和游標位置。
<requestFocus />獲得焦點。

旋轉螢幕(模擬器下按Ctrl+F11)後的效果:
 

摘自 小何才露尖尖角
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.