What is the best way to share code between actions with different base classes?

I have the following problem:
I have an abstract Activity class that allows me to call it MyAbstractActivity, which contains code that I would like to reuse (for example: standard connecting tool, common menu items, common initialization code, etc.). Usually I just used it to subclass my specific actions and did with it.

However, I sometimes need to use another supertype, such as ListActivity or MapActivity.

So the question is: how to avoid duplication of this support code inside the Activity if I need to use a different base class?

I came up with a solution based on a decorator pattern like this:
Activity decorator .

However, I see a problem with this approach: What to do with protected methods (e.g. onCreate ())? Should I introduce an additional class "bridge", which makes them public for the purpose of the decorator, in the same way as presented below (starting to look a little byzantine ...)?

edge case

Any other way?

I hope I made myself relatively clear. Thank you in advance for any feedback!

PS. Using static utility classes is not a good solution, in my opinion, since it introduces the ability to hard identify programming errors.

+4
source share
1 answer

If I understand correctly, neither fragments, nor the decorator template are pure or suitable solutions for what you want to execute. They were designed to solve other problems.

I find myself as a "supporting" code, or a "framework" of code, or "all that verbose, repeating, smooth screenshots" for static utility methods. This is not necessarily the approach that I would use in a project other than Android, but it works fine in my Android projects.

Also, be aware that you do not need to subclass ListActivity in order to have a ListView.

+2
source

All Articles