Drill-down menus on Android

I spent most of the yesterday struggling with implementing a drill-down menu. As usual, the problem is not implementing it somehow, but implementing it so that it works fast and the code looks nice.

In the activity where drill-down happens, I was starting off with getting some hierarchical JSON data off network or local cache, parsing it in POJOs and displaying the “surface level” of data in ListView. The thing I was struggling with–how do I get into deeper levels? When user picks an item, I could:

  • launch the same activity again, let it parse the data again, pick a different starting point in parsed hierarchical structure for display. Android framework would take care of animations between activities, and navigating back with “Back” button would also just work. This, however, feels awfully inefficient, parsing all the data over and over… With this implementation on real device there was indeed a noticeable lag.
  • the same as previous, except don’t parse JSON data in object hierarchy over and over again. When launching a new activity, serialize the hierarchical structure and stuff it into the intent. Serializing and deserializing data still takes time, and in practice there was still lag.
  • the same as previous, but put the parsed data in global state, e.g. in my subclass of Application object (like suggested here on SO). But this is using globals, and that feels sooooo wrong. For one thing, I’d have to clean up that cached data once I’m done with it. Another, if I wanted to drill-down on several datasets at the same time, I’d need to put several datasets in the shared state and keep track of which one to use. Gets complex.
  • Do the drill-down without leaving my activity. In this case, I need to take care of the animations and correct “Back” button behaviour. But the user experience would be instant–no extra parsing, serializing of data, no overhead associated with launching new activity.
    • With regards to animations, this response on SO got me on right track, it pointed me to direction of ViewAnimator. I ended up using its subclass, ViewFlipper. I took some inspiration from this article for doing animations. I stored my animations in XML though, instead of defining them in runtime.
    • Overriding behaviour of “Back” button is easy. On Android 2.0+ you override Activity’s onBackPressed(), on 1.6 and older you override onKeyDown().
Advertisements

2 thoughts on “Drill-down menus on Android

  1. Hi, thanks for sharing your experience.
    Can you make the code available? I’ll be working with a similar pattern and I am looking for a sample that I can use to start.

    Thanks!

Comments are closed.