Composition over inheritance Allows to add functionality into an Android Activity
. Just because we all have a BaseActivity
in our projects containing too much unused stuff. When it grows, it get unmaintainable.
Given you have an Activity
showing a list of tweets (TweetStreamActivity
) and you want add view tracking.
You could do it with inheritance and use TrackedTweetStreamActivity
from now on: public class TrackedTweetStreamActivity extends TweetStreamActivity { @Override protected void onResume() { super.onResume(); Analytics.trackView("stream"); } } more likely you would create a TrackedActivity
and extend the TweetStreamActivity
from it: public abstract class TrackedActivity extends AppCompatActivity { public abstract String getTrackingName(); @Override protected void onResume() { super.onResume(); Analytics.trackView(getTrackingName()); } } public class TrackedTweetStreamActivity extends TrackedActivity { @Override public String getTrackingName() { return "stream"; } } Both solutions work but don't scale well. You'll most likely end up with big inheritance structures: class MvpActivity extends AppCompatActivity { ... } class BaseActivity extends AppCompatActivity { ... } class BaseMvpActivity extends MvpActivity { ... } class WizardUiActivity extends BaseActivity { ... } class TrackedWizardUiActivity extends WizardUiActivity { ... } class TrackedBaseActivity extends BaseActivity { ... } class TrackedMvpBaseActivity extends BaseMvpActivity { ... }
Some libraries out there provide both, a specialized Activity
extending AppCompatActivity
and a delegate with a documentation when to call which function of the delegate in your Activity
. public class TrackingDelegate { /**
build.gradle
.
CompositeAndroid let's you add delegates to your Activity without adding calls to the correct location. Such delegates are called Plugins
. A Plugin is able to inject code at every position in the Activity lifecycle. It is able to override every method.
Date | Time |
---|---|
June 8, 2023 (Thursday) | 09:30 AM - 04:30 PM |
June 22, 2023 (Thursday) | 09:30 AM - 04:30 PM |
July 6, 2023 (Thursday) | 09:30 AM - 04:30 PM |
July 20, 2023 (Thursday) | 09:30 AM - 04:30 PM |
August 3, 2023 (Thursday) | 09:30 AM - 04:30 PM |
August 17, 2023 (Thursday) | 09:30 AM - 04:30 PM |
August 31, 2023 (Thursday) | 09:30 AM - 04:30 PM |