Sitemap
Android Dev

Personal blog of a small team of Android engineers who believe that Android development should be very simple, fast and reliable.

CheckedTextView does not save its state.

1 min readFeb 18, 2016

--

Few weeks ago I found an interesting issue after device orientation change CheckedTextView does not save checked state.

I started to research the question “Why CheckedTextView does not save its state”. The first idea that comes to my mind was “Maybe I forgot to set id?”. Because, as we know, Android does not save View state if it does not have an id. However, it was set.

Next step was to check Android sources and after some research, I found that CheckedTextView does not save its state.

This finding very surprised me because CheckedTextView exists from API 1 and it means that this issue already exists for many years.

Now I know what the problem is and correct it not be difficult. Below you can find a solution to fix this issue.

Get Viacheslav Antonenko’s stories in your inbox

Join Medium for free to get updates from this writer.

Firstly we need to create an own class that extent CheckedTextView or AppCompatCheckedTextView if you use support library.

The second step is to create a class that save and restore our state. This class must extend BaseSavedState.

So last step was save our state at onSaveInstanceState and restore it at onRestoreInstanceState.

The complete solution you can find at my gist.

Also, I created an issue at Android bug tracker.

https://code.google.com/p/android/issues/detail?id=194265

--

--

Android Dev
Android Dev

Published in Android Dev

Personal blog of a small team of Android engineers who believe that Android development should be very simple, fast and reliable.