I would like to be able to dynamically build an Activity that has a few of the capabilities that we have (such as prevent scrolling, monitors internet connectivity, prevent orientation change, etc.).
We don't want to have to create an abstract BaseActivity
that contains all possible properties and abstract methods for all capabilities. We would have a lot of empty function implementations in subclasses of BaseActivity
who only need to actually implement a few of those functions. Worse yet, if we think of another capability (another set of properties and functions to add to BaseActivity
), we would have to edit EVERY child of BaseActivity
to implement the new abstract functions (most likely going to be empty).
Another approach of BaseActivity
inheritance is to create child classes that implement only a few of the capabilities we have. But this will require us to create a child class for every combination of capabilities, which is too many even with few capabilities. Plus, this would result in lots of duplicate code for 2+ child classes that would implement the same capability.
So, I thought of implementing the Decorator Pattern, but I don't think we ever actually instantiate a new activity. We always create an Intent
and specify SomeActivity.class
in its constructor, then we call startActivity(new Intent(getApplicationContext(), SomeActivity.class));
Is there a way to intercept the actual instantiation of an Activity, perform Decorator Pattern manipulation on it, and then let the OS/Application put it on top of the stack?
If not, what other approaches are feasible?