Android视图关闭某些视图的隐式状态保留

收藏

I have custom View which is dynamically added multiple times in same activity.

Each of custom view instance has beside other stuff a TextView child. The TextView has it's ID and it is same for each view instance.

For each custom view instance I set different Text for TextView when activity is created. When I rotate the screen, the activity is recreated but now each TextView displays same Text (text is same as text of the last instance).

这是因为实例状态的保存和还原会自动保存一些信息,但是该信息显然与ID链接,在我的情况下会产生问题。

有没有办法禁用特定视图的自动状态保留?

是否有属性或设置或某些解决方法?

最佳答案

You can use View.setSaveEnabled (boolean enabled) method.

It's also possible to disable automatic state saving via xml using saveEnabled attribute android:saveEnabled="false"