3

I ran into an issue with kotlin delegation where I want to delegate the interface implementation to a variable. The problem is that the superclass has some other constructors that I would not want to lose really

interface Delegate
class CustomView(myRequiredDelegate: Delegate,context:Context): ViewGroup(context), Delegate by myRequiredDelegate

Now as you can see I will have problems with the android studio preview as there is no constructor(context: Context) And also other problems with initializing from xml as I will have to create

class View(myRequiredDelegate: Delegate,context: Context): ViewGroup(context), Delegate by myRequiredDelegate{
   // as you can see here I can no longer have access to call super(context, attrs...)
   // which provide more attrs for the custom view
   // and not sure if kotlin provides any other way to access the other super constructors
   constructor(context: Context):this(dummyForDelegate,context)
}

I just want to know if there is a way to provide the delegation as so without breaking the super call chain not specifically for android but for any other case that might have the same delegation constraints...

Breimer
  • 506
  • 8
  • 14
  • 1
    This looks to me like the best way to do it. If the interface is needed to be able to display the preview, you would need that dummy implementation anyway. – Tenfour04 Jun 06 '20 at 04:08
  • When delegating you don't implement the interface the delegate is supposed to handle that... – Breimer Jun 06 '20 at 10:41

0 Answers0