Friday, 16 November 2012

Android Preferences Tutorial

Introduction
Recently I have added a preference screen to one of my Android apps and now is a good time to put all what I have learnt down on paper, more for future reference. I'll present a quick tutorial and then discuss more advanced topics and pitfalls. Note that I will not be discussing the new Preference Fragments API, for now my Nexus 7 is more than happy running the deprecated preference APIs.

I have a CheckBoxPreference to toggle a boolean value and a ListPreference to select a string value. I've put the display strings in the strings.xml file in my values folder. You'll notice that the list preference references an array resource. You can create the array resources by creating a resource file (another Android XML file) in the values folder and call it arrays.xml. Below is my arrays.xml file:

I get the SharedPreferences object using PreferenceManager.getDefaultSharedPreferences(this), which allows the various activities of my app access to the preference data.

The Preference User Interface will fire SharedPreferenceChanged events which I listen to in my MainActivity class to keep the app up to date with the latest preferences. These events fire as soon as you change a preference, toggle a check box on the preference form and the event will fire.

Note that when the app first starts that I call LoadPreferences() this is important as it sets up my app with the current preference information.

Application Life-Cycle Pitfalls

Be careful if you use anonymous inner classes to implement the OnSharedPreferenceChangeListener. You'll notice that in the code above I register the MainActivity class in OnCreate() and unregister in OnDestroy(). This is fine if the MainActivity object is the listener. If you use anonymous classes then these can be collected by the garbage collector when the app is paused, this means that your app will then stop responding to SharedPreferenceChanged events. For more details see this StackOverflow Question about the issue.

About Me

Pig Dog Bay was born in an inn deep in the Staffordshire moorlands. He lived in a small box under the pool table and was fed on pork scratchings left in the ash trays.

One day a raucous rock band called Motorhead played at the inn causing mayhem, so Pig Dog spied his chance and had it away on his trotters.

The nights were cold and after many days of trotting, a weary Pig Dog took shelter in a green house where big ripe tomatoes grew. The next day the owners son spotted Pig Dog and took pity on the hungry creature. Together they played computer games on a dusty old ZX Spectrum, eventually the son left and Pig Dog was all alone. Dabbing at the rubber keys, Pig Dog had made a computer program:

PIG DOG BAY

PIG DOG BAY

PIG DOG BAY

Whizzed up the screen. This pleased Pig Dog greatly. Many years later the son returned and was so happy to see his old friend, he gave Pig Dog his Google Nexus 7. Pig Dog squealed with glee and now wanted to show the world his little apps.