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

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

问题描述

我正在尝试保存和恢复由按钮表组成的视图层次结构.表中所需的表行数和按钮数直到运行时才知道,并以编程方式添加到我的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?

下面是我当前尝试的一个示例.在最初运行时,该表将按预期的方式构建.当活动被破坏(通过旋转设备)时,重建的视图仅显示一个空的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);
       }
    }
}

我的理解是,只要为视图分配了ID,Android便会保存视图状态,并在重新创建活动时恢复视图,但目前看来,它只是重新构造了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天全站免登陆