Is there a design pattern to cut down on code duplication when subclassing Activities in Android?
Posted
by Daniel Lew
on Stack Overflow
See other posts from Stack Overflow
or by Daniel Lew
Published on 2010-04-06T18:07:44Z
Indexed on
2010/04/06
18:13 UTC
Read the original article
Hit count: 202
I've got a common task that I do with some Activities - downloading data then displaying it. I've got the downloading part down pat; it is, of course, a little tricky due to the possibility of the user changing the orientation or cancelling the Activity before the download is complete, but the code is there. There is enough code handling these cases such that I don't want to have to copy/paste it to each Activity I have, so I thought to create an abstract subclass Activity itself such that it handles a single background download which then launches a method which fills the page with data.
This all works. The issue is that, due to single inheritance, I am forced to recreate the exact same class for any other type of Activity - for example, I use Activity, ListActivity and MapActivity. To use the same technique for all three requires three duplicate classes, except each extends a different Activity.
Is there a design pattern that can cut down on the code duplication? As it stands, I have saved much duplication already, but it pains me to see the exact same code in three classes just so that they each subclass a different type of Activity.
© Stack Overflow or respective owner