Common tasks in Activities methods - how to organize them?
I have some common actions fired in onPause()
and onResume()
methods. (Like registering and unregistering BroadcatsReceivers
)
Now I put them in abstract classes and extend Activity classes.
After creating some abstract classes with common actions I end up with situation when I can't extend Activity because of Java's lack of multiple inheritance.
How you deal with such things? Do you duplicate code or do something smarter?
I'm wondering if it's wider problem - not only concerning Android, but 开发者_运维问答Java language.
Task A and Task B are two separate tasks. But to be used in just one class. So you can just have a single abstract class. The abstract may not be necessary if you feel that there are no functions that will have to be overridden in the child class.
If for some reason there may be a task D, which uses Task A alone, then you can do the following:
public abstract class ExtendedAbstractActivityA extends Activity
{
public void TaskA() {}
public void TaskB() {}
//Other abstract classes
}
You can call these tasks individually from your respective classes.
精彩评论