How Do I Do This - same overrides for descendant classes which I cannot control

Hi all,

I know I’ve read somewhere a bit about this, but I cannot find it anymore. I’ll explain what I want to do:

In an Android app, I use the classes Fragment and ListFragment very often. Listfragment is some descendant class of Fragment. Both classes I cannot control, of course.

Now I want to add some basic functionality (like overriding onStart etc) to all my own classes, at the level of Fragment. For normal code, I could use extension methods. But how to do the override for the onStart?

Any tips?

Hugo,
I think that the only solution is to make your own MyFragment and MyListFragment classes that inherit, repectively, from Fragment and ListFragment. Each of these base classes will override their own onStart method.
Now, if you have common code, or the same code, on both MyFragment and MyListFragment, you can define it as a static method on one of them and use it on the other. Or your can define extension methods and use them on both of your classes.
Patrick

Hi Patrick, that’s how I’ve done yet (the plain old Delphi way). I think I somewhere saw a sample using some sort of generics to handle this nicely. Something like this.

Type CommonFragmentClass  = public class (FragClass) where FragClass is Fragment;
public
  method onStart; override; 
end;

myMainScreenClass = public class CommonFragmentClass
public
...
end;

Hugo,
I don’t see how it could work this way, because to override Fragment.onStart you really need to inherit you class from Fragment and, for the ListFragment.onStart, you need to inherit from List. So you will need to insert you own class between Fragment and ListFragment and it’s not possible.
Patrick

@Patrick, ListFragment is a direct descendant from Fragment, see http://developer.android.com/reference/android/app/ListFragment.html .
So as long as FragClass descends from Fragment this should be no problem? Or maybe I do not understand the point you want to make?

Hugo,
I you want to override onStart in Fragment and have the override work on ListFragment, you need to have a class you define in between, and it’s not possible:

  Fragment = public class (...)  // defined in Java
  MyFragment = public class (Fragment)  // defined yourself
  ListFragment = public class (MyFragment)  // not possible

Patrick

@patrick, that’s exactly the problem why I posted the question, with the most important aspect: how to prevent double identical code.

Like Patrick said: why not like this:

  MyFragment = public class (Fragment)
  assembly
    class method onStartHelper(aFragment: Fragment);
  public
    method onStart; override; 
  end;

  MyListFragment = public class (ListFragment)
  public
    method onStart; override; 
  end;

implementation

class method MyFragment.onStartHelper(aFragment: Fragment);
begin
  //do common stuff, you can use the current instance through aFragment
end;

method MyFragment.onStart;
begin
  inherited;
  onStartHelper(self);
end;

method MyListFragment.onStart;
begin
  inherited;
  MyFragment.onStartHelper(self);
end;

Hi jeroen,

this is the way I have it working now. The problem arises for protected properties etc which cannot be accessed this way. And of course still doing things twice. Not a very big problem, but I was just wondering if Oxygene would provide an even better solution, like the kind-of-generics solution…

Thanks for you input…

Hugo