caipivara
7/28/2016 - 2:58 PM

Android / Kotlin - Using @JvmOverloads tag to avoid multiple "empty" constructors for inheritance (shown with custom android views). (thanks

Android / Kotlin - Using @JvmOverloads tag to avoid multiple "empty" constructors for inheritance (shown with custom android views). (thanks to https://discuss.kotlinlang.org/t/simple-constructors-for-inheritance/1874/2)

class MyCustomView : FrameLayout {
  
  constructor(context: Context) : super(context) {
    init()
  }

  constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    init()
  }

  constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int)
  : super(context, attrs, defStyleAttr) {
    init()
  }

  fun init() { 
    // ...
  }
}
class MyCustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0)
: FrameLayout(context, attrs, defStyleAttr){

  init {
    // ...
  }
}