I’m creating a few custom preferences for an Android application.
The two I’m creating is a SeekBarPreference and a MultiSelectPreference.
The SeekBarPreference is quite simple, it inherits from DialogPreference and has a custom content view in which it displays a SeekBar.
The MultiSelectPreference is a bit more advanced, it opens a Dialog when clicked which has a custom ListView and a button. When the button is pressed another Dialog is opened which has a generic ListView.
What should I save in onSaveInstanceState() for these preferences?
For the SeekBarPreference I assumed that I needed to save the value if isPersistant() is false and also the current slider progress (so I could restore) but after browsing the android source code doesn’t seem like EditTextPreference cares about storing it’s current text, only the saved value.
The DialogPreference does however save the current state of the dialog but it doesn’t seem like that saves the state for its children, but it’s quite hard to tell, I got lost when I came to what a Window will do when onSaveInstanceState is called since it’s abstract and I didn’t find any subclasses.
Any insight to this problem would be greatly appreciated, my goal is to open source the code later so I want to do this right.
After a lot of testing and code browsing these are my conclusions:
Even though the last point is not followed by Google them selves they are wrong and an easy example shows why:
Imagine you have a PreferenceActivity which contains two things; a
ListPreferencewith 10 items and a generic preference that when pressed adds an item to the end of the ListPreference.Now imagine that the user starts the application and presses the generic preference, the ListPreference now has 11 items.
The user now opens the ListPreference.
If the application is closed and then killed the instance is saved, but the ListView only saves the stored value (if it isn’t persistant), so when it reopens it only contains the initial 10 item.
Below is an example of a minimal app that displays the problem, just click on the button, open the list (there should be 4 items), close the app with the list open, kill the app from adb and reopen it, the list now has three items.
PreferenceTest.java
res/xml/preferences.xml
res/values/list_items.xml