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?
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"
if you need apply setSaveEnabled for all hierarchy:
public static void SaveEnableViewAndChildren(this View view, bool val) { if (view == null) return; view.SaveEnabled = val; var viewGroup = view as ViewGroup; if (viewGroup == null) return; for (int i = 0; i < viewGroup.ChildCount; i++) SaveEnableViewAndChildren(viewGroup.GetChildAt(i), val); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With