onRestoreInstanceState() and Unique View Ids

613 views Asked by At

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.

2

There are 2 answers

2
corsair992 On BEST ANSWER

AdapterView (the base class for all ViewGroups that utilize an Adapter) is implemented so as to not save or restore the state of it's child Views, as that is the domain of the Adapter and should be handled at that level.

As for the case of a sublayout that is statically included at multiple points, if it has any state to be saved then you will need to either assign unique ids to all stateful Views, or manage the state manually from the Activity or Fragment.

Unfortunately, the ids need to be unique across the whole View structure, instead of just among it's siblings. This is due to the fact that the state of the whole structure is saved inside one single-dimensional SpareArray. Thus if there are multiple stateful Views in the structure sharing the same id, then only the state of the last one in the structure will be saved, and it will be applied to all the Views with that id upon state restoration.

0
htafoya On

One simple solution can be to assign ID programatically.

Ids are not inmutable, so you may change the ids during your view creation.

Let's say you inflate some views MyCustomView which have some EditText named nameEditText. At the view creation you could have the following:

binding.view1.nameEditText.id = R.id.pageX_field1
binding.view2.nameEditText.id = R.id.pageX_field2

This way the save state will store the values based on the given ids. The restoration of state will happen after the creation of the view.

You may create your ids on a resource file for this purpose:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <!-- PAGE X IDS -->
    <item type="id" name="pageX_field1" />
    <item type="id" name="pageX_field2" />

</resources>