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

scruffyfox

08/14/2017, 1:15 PM
is it possible to have non-argument and argument constructors using the constructor syntax example:
class DownloadHelper(internal val tag: String = "") : Fragment()
k

kingsley

08/14/2017, 1:16 PM
class DownloadHelper @JvmOverloads constructor(internal val tag: String = ""): Fragment()
But you shouldn’t be doing this with android Fragments. The framework will always call the no-arg constructor instead, so you might end up with more side effects that necessary
s

scruffyfox

08/14/2017, 1:18 PM
i can just create multiple constructors no problem (or newInstance method) just wondering if there was a cool kotlin way of doing it
k

kingsley

08/14/2017, 1:21 PM
The issue is not with Kotlin. It is with having multiple constructors for a Fragment. The framework always calls the one with no-arg, so even if you start the fragment using a constructor with arguments, the framework could later recreate teh fragment, and it will use the no-arg one instead
A
newInstance
(or whatever name) factory method is much better
s

scruffyfox

08/14/2017, 1:22 PM
yes my question was purely syntactical 🙂
k

kingsley

08/14/2017, 1:22 PM
Ah. Okay then 🙂
s

scruffyfox

08/14/2017, 1:23 PM
cheers for the help anyway
k

kingsley

08/14/2017, 1:23 PM
You’re welcome
4 Views