• Api demo源码学习(12)App/Activity/Save & Restore State


    这一节蛮有意思,看我最初为这一节写的开头:

    “让我诧异的是,Api demo的这一节似乎带有bug,按照原来的意图输入的文本信息会被保存,但我在虚拟机和HTC手机上多番尝试,确定这预订的功能并没有实现。按照注释的说明,应该是调用onSaveInstanceState()函数来实现信息的保存的。”


    当时写这一节的时候再公司没法上网,就先放着。回来一查,网上果然还有其他人也在抱怨这一节功能似乎没有实现,老外中国人都有。但到底还是有牛人的,这个例子如果是按了返回键,那上下两个文本框的内容都不见了,没有被保存下来(这也是我误以为程序有bug的原因),但其实这个例子是要说明生命周期切换中数据的保存问题。先修改两个文本框中的文字内容,然后把手机翻转,竖屏切为横屏,就会看到上面的文本框中文字内容没有变,下面的文本框中的内容又变为初始值了。

    onSaveInstanceState()会在生命周期变化的过程中自动执行,在layout中只要指定了id的View都会被保存和恢复。在待会的代码中我们也会看到上面的EditText 指定了ID,下面的EditText没有指定ID。既然指定了ID就可以自动保存,那在源代码中的这两句:
        CharSequence getSavedText() {
            return ((EditText)findViewById(R.id.saved)).getText();
        }
        void setSavedText(CharSequence text) {
            ((EditText)findViewById(R.id.saved)).setText(text);
        }

    这两句又是起什么作用的呢?我把这两句注释掉,仍然不影响代码功能,这就让我纳闷了。暂时不清楚原因。

    下面是源码:
    View Code
     1 public class SaveRestoreStateActivity extends Activity {
    2 /** Called when the activity is first created. */
    3 @Override
    4 public void onCreate(Bundle savedInstanceState) {
    5 super.onCreate(savedInstanceState);
    6 setContentView(R.layout.save_restore_state);
    7 ((TextView)findViewById(R.id.msg)).setText(R.string.save_restore_msg);
    8 }
    9
    10 CharSequence getSavedText() {
    11 return ((EditText)findViewById(R.id.saved)).getText();
    12 }
    13
    14 /**
    15 * Change the text that is currently in the "saved" editor.
    16 */
    17 void setSavedText(CharSequence text) {
    18 ((EditText)findViewById(R.id.saved)).setText(text);
    19 }
    20 }

    save_restore_state.xml
    View Code
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    2 android:layout_width="match_parent" android:layout_height="match_parent">
    3
    4 <TextView android:id="@+id/msg"
    5 android:layout_width="match_parent" android:layout_height="wrap_content"
    6 android:layout_weight="0"
    7 android:paddingBottom="4dip" />
    8
    9 <TextView
    10 android:layout_width="match_parent" android:layout_height="wrap_content"
    11 android:layout_weight="0"
    12 android:paddingBottom="4dip"
    13 android:text="@string/saves_state"/>
    14
    15 <EditText android:id="@+id/saved"
    16 android:layout_width="match_parent" android:layout_height="wrap_content"
    17 android:layout_weight="1"
    18 android:text="@string/initial_text"
    19 android:freezesText="true">
    20 <requestFocus />
    21 </EditText>
    22
    23 <TextView
    24 android:layout_width="fill_parent" android:layout_height="wrap_content"
    25 android:layout_weight="0"
    26 android:paddingTop="8dip"
    27 android:paddingBottom="4dip"
    28 android:text="@string/no_saves_state"/>
    29
    30 <EditText
    31 android:layout_width="fill_parent" android:layout_height="wrap_content"
    32 android:layout_weight="1"
    33 android:text="@string/initial_text">
    34 </EditText>
    35
    36 </LinearLayout>

    以上即可。



  • 相关阅读:
    回顾2011,展望我的2012
    查看MS SQL SERVER数据库中表的大小
    MS SQL SERVER数字格式化显示,每三位加逗号
    MS SQL Server 保留一行,清除多余冗余数据
    ASP.NET Webform和ASP.NET MVC的区别
    Firefox的刷新功能与Safari,IE的差距
    TIOBE如何计算编程语言的排行?
    如何让ComboBox的下拉列表宽度自适应内容的宽度
    如何启用.NET中的Fusion Log
    JavaScript的clone函数的实现及应用条件
  • 原文地址:https://www.cnblogs.com/xutao1988/p/2288075.html
Copyright © 2020-2023  润新知