Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android view turn off implicit state retaining for some view

Tags:

java

android

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?

like image 967
Dusan Avatar asked Mar 20 '13 15:03

Dusan


Video Answer


2 Answers

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"

like image 75
Vladimir Mironov Avatar answered Oct 04 '22 17:10

Vladimir Mironov


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);     } 
like image 37
Danil Shaykhutdinov Avatar answered Oct 04 '22 17:10

Danil Shaykhutdinov