从保存的状态恢复视图层次结构不会恢复以编程方式添加的视图 [英] Restoring view hierarchy from saved state does not restore views added programmatically

查看:11
本文介绍了从保存的状态恢复视图层次结构不会恢复以编程方式添加的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存和恢复由按钮表组成的视图层次结构.表格中所需的表格行和按钮的数量直到运行时才知道,并在我的 ActivityonCreate(Bundle) 方法中以编程方式添加到膨胀的 xml 布局.我的问题是:可以使用 Android 的默认视图保存/恢复实现来保存和恢复最终表吗?

I am attempting to save and restore a view hierarchy consisting of a table of buttons. The number of table rows and buttons required in the table is not known until runtime, and are added programmatically to an inflated xml layout in my Activity's onCreate(Bundle) method. My question is: can the final table be saved and restored using Android's default view saving/restoring implementation?

我当前尝试的示例如下.在初始运行时,表按预期构建.当 Activity 被销毁(通过旋转设备)时,重建的视图只显示一个空的 TableLayout,没有子元素.

An example of my current attempt is below. On the initial run, the table builds as expected. When the activity is destroyed (by rotating the device), the rebuilt view shows only an empty TableLayout with no children.

setContentView(int) 中引用的 xml 文件包括添加按钮的空 TableLayout.

The xml file referenced in setContentView(int) includes, among other things, the empty TableLayout that the buttons are added to.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Setup this activity's view.
    setContentView(R.layout.game_board);

    TableLayout table = (TableLayout) findViewById(R.id.table);

    // If this is the first time building this view, programmatically
    // add table rows and buttons.
    if (savedInstanceState == null) {
        int gridSize = 5;
        // Create the table elements and add to the table.
        int uniqueId = 1;
        for (int i = 0; i < gridSize; i++) {
            // Create table rows.
            TableRow row = new TableRow(this);
            row.setId(uniqueId++);
            for (int j = 0; j < gridSize; j++) {
                // Create buttons.
                Button button = new Button(this);
                button.setId(uniqueId++);
                row.addView(button);
            }
            // Add row to the table.
            table.addView(row);
       }
    }
}

我的理解是,Android 保存视图的状态,只要它们有分配给它们的 ID,并在重新创建 Activity 时恢复视图,但现在它似乎重新膨胀了 xml 布局,仅此而已.在调试代码时,我可以确认在表中的每个 Button 上都调用了 onSaveInstanceState(),但没有调用 onRestoreInstanceState(Parcelable).

My understanding is that Android saves the state of views as long as they have an ID assigned to them, and restores the views when the activity is recreated, but right now it seems to reinflate the xml layout and nothing more. When debugging the code, I can confirm that onSaveInstanceState() is called on each Button in the table, but onRestoreInstanceState(Parcelable) is not.

推荐答案

搜索ActivityViewViewGroup的源码后,我了解到每次调用 onCreate(Bundle) 时,必须以编程方式添加视图并分配相同的 ID.无论是第一次创建视图,还是在销毁活动后重新创建视图,都是如此.然后在调用 Activity.onRestoreInstanceState(Bundle) 期间恢复以编程方式添加的视图的已保存实例状态.对上述代码最简单的答案就是删除对 savedInstanceState == null 的检查.

After searching through the source code for Activity, View and ViewGroup, I learned that the programmatically added views must be programmatically added and assigned the same ID each time onCreate(Bundle) is called. This is true regardless of whether it is creating the view for the first time, or recreating the view after destroying the activity. The saved instance state of the programmatically added views is then restored during the call to Activity.onRestoreInstanceState(Bundle). The easiest answer to the code above is simply to remove the check for savedInstanceState == null.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Setup this activity's view. 
    setContentView(R.layout.game_board); 

    TableLayout table = (TableLayout) findViewById(R.id.table); 

    int gridSize = 5;
    // Create the table elements and add to the table. 
    int uniqueId = 1; 
    for (int i = 0; i < gridSize; i++) { 
        // Create table rows. 
        TableRow row = new TableRow(this); 
        row.setId(uniqueId++);
        for (int j = 0; j < gridSize; j++) {
            // Create buttons.
            Button button = new Button(this);
            button.setId(uniqueId++);
            row.addView(button);
        }
        // Add row to the table.
        table.addView(row);
   }
}

这篇关于从保存的状态恢复视图层次结构不会恢复以编程方式添加的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆