Another Android Blog

Insights into those hard to solve Android Development problems

Skip to: Content | Sidebar | Footer

Extending from Two Activities (sort of)

3 January, 2013 (15:03) | Uncategorized | By: Randall Mitchell

There are generally very few reasons to need to mimic multiple inheritance of activities. One example that *may* find use of this sort of thing is a situation where a library that has a base activity is used when the project already has a base class. Generally, you will want your base class to extends the library activity, but sometimes that is not possible. Then, it *may* be efficient to use the code I present here. Beat this code with a yard stick before considering its use. There is probably an alternative solution that is more elegant. Do you need to feel that your code in both acitivies must be tied to those activities or can code from one of the activies fall into a worker class that handles this extra code? It will probably be easier for the next guy to figure out what you did if you use a worker class instead.

ORIGINAL BASE ACTIVITES

// If we want to inherit from both of the below classes.
// methodThree has an argument to help understand how the arguments will work.
public class BaseActivityAlpha extends Activity {
    public void methodOne() {
    	// do something
    }
 
    public void methodTwo() {
    	// do something else
    }
}
public class BaseActivityBeta extends Activity {
    public void methodThree(int argument) {
        // do a third thing
    }
 
    public void methodFour() {
    	// do a fourth thing
    }
}

UPDATED BASE ACTIVITES

Create an interface for BaseAcitivtyB to some other class and move the code to some other class. Notice the interface methods accept a reference to an activity. This is because a lot of methods inside of an Activity require calls to the superclass Activity. Also notice the use of two interfaces to help ensure no methods are left behind. Making the conversion to BaseActivityB will not break any existing code either.

public class BaseActivityAlpha extends Activity {
    public void methodOne() {
    	// do something
    }
 
    public void methodTwo() {
    	// do something else
    }
}
public class BaseActivityB extends Activity implements BaseActivityBetaI {
    private BaseActivityBetaImplementation mImplementation;
    @Override public methodThree(int argument) {
        mImplementation.methodThree(this, argument);
    }
 
    @Override public methodFour(int argument) {
    	mImplementation.methodFour();
    }
 
}
public interface BaseActivityBetaI {
    public void methodThree(int argument);
    public void methodFour();
}
public interface BaseActivityBetaImplementationI {
    public void methodThree(Activity activity, int argument);
        public void methodFour(Activity activity);
    }
}
public class BaseActivityBetaImplementation implements BaseActivityBetaImplementationI {
    @Override public void methodThree(Activity activity, int argument) {
 	// do a third thing
    }
 
    @Override public void methodFour() {
	// do a forth thing
    }
}

NEW ACTIVITY THAT GETS FUNCTIONALITY FROM WHAT WAS TWO OTHER ACTIVITES

Notice that the following activity looks almost identical to BaseActivityB. We still have duplicate code; however, any implementation is only written once.

public class ActivityOfActivities extends BaseActivityAlpha implements BaseActivityBetaI {
    private BaseActivityBetaImplementation mImplementation;
 
    // methodOne and methodTwo are inherited
 
    @Override public methodThree(int argument) {
    	mImplementation.methodThree(this, argument);
    }
 
    @Override public methodFour(int argument) {
    	mImplementation.methodFour();
    }
 
    private void methodFive() {
    	// do my own thing
    }
}