https://kotlinlang.org logo
#android
Title
# android
k

kevinmost

08/17/2017, 3:25 PM
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

skennedy

08/17/2017, 9:28 PM
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

kevinmost

08/17/2017, 10:05 PM
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

skennedy

08/17/2017, 10:08 PM
well, if you want to pass in a
defStyleAttr
you need the 3 arg