Monday, September 5, 2011

Using a Base ListActivity for Different Atom Feeds

In the Connexions for Android app, there are 3 different Atom feeds that are used to display 3 different types of Lenses. A Lens is a group of Connexions content that is endorsed by or affiliated with a particular group. Users of Connexions can also create their own Lens to store content they find useful. Each of these types (Endorsement, Affiliation, Member) have an Atom feed that is used to display the list of Lenses in the Android app. I didn't want to have 3 separate Activities since their functionality is the same for each type of lens so I created a base Activity with the needed functionality and then extended it for each type of lens. The subclasses contain the unique data needed for each type.

The base Activity has the cleaver name of BaseLensesActivity. It does not do anything out of the ordinary for a ListActivity, but it has 4 Class variables that are the key to the subclasses being able to extend BaseLensesActivity. The 4 fields are currentContext, title, storedKey and atomFeedURL. These 4 fields are overridden by the subclasses so the correct data is stored and displayed.

Details of the 4 fields:
  • currentContext - used when the Context is passed to the MenuHandler object.  It is set to the current subclass.
  • title - the title displayed on the activity.  It is set to the Lens type.
  • storedKey - used when storing the list of lenses in the ContextCache object.  Lists are stored when the Activity is paused so it does not have to be reloaded from the Connexions site.
  • atomFeedURL - the URL for the Feed for the Lens type.
One method also had to be overridden, onOptionsItemSelected().  I'm not sure why, but the method would not work correctly without being overridden.  No code was changed in the overridden version, just the location of the code.

You can browse the source or download a zip file of the source. Connexions for Android is available in the Android Market or from the Connexions website.