I use a live-template in Kotlin for this... ``` c...
# android
k
I use a live-template in Kotlin for this...
Copy code
class $NAME$ @JvmOverloads constructor(
        context: Context,
        attrs: android.util.AttributeSet? = null,
        defStyleAttr: Int = 0
) : $SUPER$(context, attrs, defStyleAttr) {


    init {
        $END$
    }
}
😨 1
👍 3
s
This is dangerous and can break even the most basic views (like
TextView
). TextView’s 2 arg constructor passes in a non-zero
defStyleAttr
to its own 3-arg constructor. Your 2 arg constructor should always call the super 2 arg constructor.
k
You're right. That's a good point. I think I'll modify my template to only call the 2-arg constructor. Are there situations where you need a 3-arg constructor? If someone actually needs it, they can always add it as a secondary
constructor
. Or I can add that as part of my template
s
well, if you want to pass in a
defStyleAttr
you need the 3 arg