1

Is following possible:

1) Somewhere I define something like:

private static enum MODE
{
    ANDROID,
    HOLO_EVERYWHERE
}

public static final MODE = MODE.ANDROID;

2) use this definitions and make some CUSTOM code, like following (or similar, or just somehow else, this code is just an example to demonstrate what I want... something like #ifdef in C...):

if (MODE == MODE.ANDROID)
    include android.app.Activity as ACTIVITY;
else
    include com.WazaBe.HoloEverywhere.sherlock.SActivity as ACTIIVTIY;
public ExtendedActivity extends ACTIVITY
{
    public ExtendedActivity()
    {
        if (MODE == MODE.ANDROID)
            this.callFunction1();
        else
            this.callFunction2();
    }
}

EDIT

My goal is following:

I don't want to wrap the two classes because I want my library to work without the other library (like the HoloEverywhere library) installed... I don't want a user of my library either change my code or include the HoloLibrary...

I want him to be able to set up which base class to use and that's it...

prom85
  • 16,896
  • 17
  • 122
  • 242
  • 1
    why not create 2 classes `ExtendedAndroidActivity` and `ExtendedHoloActivity` and then choose which one to use depending on your current MODE ? – sdabet Aug 02 '13 at 07:25
  • Possibly relevant: http://stackoverflow.com/questions/1813853/ifdef-ifndef-in-java – rutter Aug 02 '13 at 07:29
  • @fiddler I added an edit to explain, why I don't want to use that... – prom85 Aug 02 '13 at 07:31

2 Answers2

2

Yes it's possible to act like that, but not outside of the Class-Members. But it's not possible to cast an extended class 1. Outside of it, 2. the Classmembers have to be registred before running the Programm and that's not possible in this way.

public ExtendedActivity extends ACTIVITY
{
    public ExtendedActivity()
    {
        if (MODE == MODE.ANDROID) {
            include android.app.Activity;
            this.callFunction1();
        }
        else {
            include com.WazaBe.HoloEverywhere.sherlock.SActivity;
            this.callFunction2();
        }
    }
}

Otherwise you could use Reflections for exactly this Problem. See also: http://docs.oracle.com/javase/tutorial/reflect/

JavaDM
  • 851
  • 1
  • 6
  • 29
  • so this means, I can use custom functions with the static final definid variable IN my class, if I call the includes in there as well, but I cannot extend my class from a custom parent class? – prom85 Aug 02 '13 at 08:03
  • Actually, I would like to avoid reflection for such a simple thing... from the logical side, this could be easily done by the compiler... btw, Reflection would only work in combination with a wrapper, wouldn't it? Extending an unknown class would not work either... – prom85 Aug 02 '13 at 08:33
  • With reflection, you can instantiate classes or subclasses on realtime. The compiler has only to know which type of ownerclass or superclass they are. You can easily manage that with implementing an Interface on each class, where the names are equal but the functionality changes. Read a little bit about reflections. Its a very big but useful thing ;) – JavaDM Aug 02 '13 at 09:25
  • Actually, I just tried that and it seems to work... but that's actually to much work for what I need it... I will stay publishing my library with the base classes I use myself and everyone else has to do the replacing of the class himself... it's anyway not that much work... thanks for the help though... – prom85 Aug 02 '13 at 10:12
0

You can have 2 activities (for a single activity extending 2 different classes) and can launch the activities conditionally.

if (MODE == MODE.ANDROID)
    startActivity(A)
else
    startActivity(B)

But your launcher activity has to be one, that cannot be designed in this way.

I was wondering why are you trying to do this ??

Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43
  • I added an comment to explain, why this is no solution for me and why I want to do it in another way... – prom85 Aug 02 '13 at 07:33