Is there a design pattern to reduce code duplication when subclassing in Android?

I have a common task that I do with some actions - loading data and then displaying it. I have a boot part down to stroke; this, of course, is a bit complicated due to the possibility of the user changing the orientation or undoing the action before the download is complete, but there is code. There is enough code to handle these cases, so I don’t want it to copy / paste it into every action that I had, so I decided to create an abstract subclass object so that it handles one background load, which then runs a method that fills the page with data.

It all works. The problem is that due to single inheritance, I have to recreate the same class for any other type of Activity - for example, I use Activity, ListActivity and MapActivity. To use the same method for all three requires three repeating classes, except that each of them extends a different activity.

Is there a design pattern that can reduce code duplication? Be that as it may, I have long kept a lot of duplicates, but it pains me to see the same code in three classes so that each subclass has a different type of Activity.

Edit: since it seems to me that I need to be more specific ...

Suppose I am trying to solve the problem of loading an AsyncTask soundtrack during orientation changes. The solution I have right now is to use callbacks; there is a download manager that I have that runs these downloads, and then I have callback related activity. When the orientation changes, the action is destroyed and then recreated; during this process, I disconnect the old Activity callback, and then attach the new callback from the new action.

Orientation changes are a common problem, and in a few actions I run Activity with a view of the progress when loading data. What I'm trying to solve is not to repeat this orientation control logic ten times; my original solution was a subclassical action, but then I got the problem above.

+5
4

. , (), Activity, ListActivity MapActivity.

+3

, . , AsyncTask, , . , - SD-, , .

Activity.

, , . , onCreate....

0
0

Is there any way for you to have a helper class with static methods to which you will pass your actions and do common work there, and not in every action?

0
source

All Articles