简体   繁体   中英

Activity onSaveInstanceState in the absence of view IDs

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.

You don't needs ids does not mean you should never use IDs. That is for the context of updating your views. There are many other cases where ids are useful like rules in a RelativeLayout.

So if state restoration is important for your use case (eg you model takes a while to recover), by all means, put ids and disable binding until your data is ready.

My question is: How does the absence of View IDs affect state save/restore?

To answer your question directly, the absence of a view ID will prevent onSaveInstanceState from restoring that view. Android's documentation still holds true for views without ID's.

Any views that do not have ID's will still be recreated but they won't have their state restored.

I just tested this in a simple app and the views without IDs did not have their states restored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM