Activity onSaveInstanceState in the absence of view IDs

时间:2015-10-06 08:18:18

标签: android data-binding

The default implementation of Activity#onSaveInstanceState says:

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id

The Android Data binding guide, and this video - slide 9 suggests that we no longer need View IDs when using Data Binding. My question is: How does the absence of View IDs affect state save/restore?

Arguably, when using data-binding one shouldn't depend on the default mechanism to save/restore state, instead depending on the ViewModel bound to the layout to have the most up-to-date state, but I'm still curious to know about the implications.

2 个答案:

答案 0 :(得分:1)

您不需要ID并不意味着您永远不应该使用ID。这是为了更新您的视图。还有许多其他情况,其中id类似于RelativeLayout中的规则。

因此,如果状态恢复对您的用例很重要(例如,您的模型需要一段时间才能恢复),请务必使用ID并禁用绑定,直到您的数据准备就绪。

答案 1 :(得分:0)

  

我的问题是:缺少视图ID会如何影响状态保存/恢复?

要直接回答您的问题,缺少视图ID会阻止onSaveInstanceState恢复该视图。对于没有ID的视图,Android的文档仍然适用。

任何没有ID的视图仍会被重新创建,但它们的状态不会恢复。

我刚刚在一个简单的应用程序中对此进行了测试,没有ID的视图没有恢复其状态。