android存储结构图,android – 从已保存状态恢复视图层次结构...

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

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

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

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,并在重新创建活动时恢复视图,但现在它似乎重新定义了xml布局,仅此而已.在调试代码时,我可以确认在表中的每个Button上调用onSaveInstanceState(),但是onRestoreInstanceState(Parcelable)不是.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值