从保存状态恢复视图层次结构不恢复的观点加入编程 [英] Restoring view hierarchy from saved state does not restore views added programatically

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

问题描述

我试图保存和恢复视图层次组成的按钮的表。在表中所需的表行和按钮的数量并不知道,直到运行时,和以编程方式添加到一个充气XML布局在我的活动的onCreate(捆绑)方法。我的问题是:能在决赛桌被保存并恢复使用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);
       }
    }
}

我的理解是,Android的节省了意见状态,只要他们有分配给他们一个ID,并恢复了意见活动时重建,但现在它似乎reinflate的XML布局,仅此而已。当调试code,我可以证实,的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.

推荐答案

通源$ C ​​$ C搜索活动后查看的ViewGroup ,我才知道,编程方式添加的意见,必须以编程方式添加每次分配相同的ID 的onCreate(捆绑) 被调用。这是真实的,不管它正在创建的视图的第一时间,或者重新创建视图破坏活动之后。的编程方式添加的看法保存的实例状态调用 Activity.onRestoreInstanceState(捆绑)中,然后恢复。最简单的答案code以上是简单地删除检查 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天全站免登陆