The android developers guide says that Views
in your activity need to have unique ids in order to restore them in onRestoreInstanceState()
. This is because onRestoreInstanceState()
works by making a SparseArray
with ids as keys.
I find this extremely strange because so many standard Android approaches encourage reuse of ids. For example, all of the constructors for an ArrayAdapter
take a resourceId as parameter, so that the same xml resource is inflated for all children in a ListView
. Similarly if you use <include layout ...>
in your xml, you are reusing view ids.
So unless developers aren't actually using the the standard, recommended, approaches I can't see that there are many apps out there actually meeting the requirement that all Views
have unique ids.
Is this conclusion wrong? Also, what is the behaviour of onRestoreInstanceState
if the ids are not all different, and is this behaviour acceptable?
EDIT
The relevant quotes appear on the page http://developer.android.com/training/basics/activity-lifecycle/recreating.html and read as follows:
Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.
Caution: Always call the superclass implementation of onRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.