Imagine that I have a camera app where I show a small MapView in one corner showing where is the device located. So I have to extend my activity from a MapActivity to do that.
Then imagine that I want to release my app in a device without Google Apis, like the Kindle, for instance. As the MapActivity does not exist anymore, I should extend from a regular Activity.
I know that I can set
<uses-library
android:name="com.google.android.maps"
android:required="false" />
To let the user install the app even if the library is missing. And then use
try {
Class.forName("com.google.android.maps.MapView");
googleMapsEnabled = true;
} catch( ClassNotFoundException e ) {
googleMapsEnabled = false;
}
to check if the MapView class is present or not.
But the problem is the base class that I extend from, it should be Activity instead of MapActivity if Google Maps are not available.
My question is, how can I use the same code to support both options, without duplicating the whole code into different classes depending on the installed libs?
For what it’s worth, my usual approach with things like this (ie: where I have to inherit from two different base classes in different scenarios, but ultimately want the same – or nearly the same – behavior) is to break out as much as I can of the common functionality into a static helper class that I call from both classes.