Dusan Dusan - 12 days ago 5
Android Question

Android view turn off implicit state retaining for some view

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).

This is because the save and restore of instance state saves some information automatically, but this information is obviously linked to IDs and in my case creates the problem.

Is there a way to disable automatic state retaining for particular views?

Is there a property or setting or some workaround?

Answer

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"