Add optional parameter to Android Kotlin class

Viewed 27298

I've created an extension of DialogFragment():

class AlertDialogFragment(context: Context, 
                          val positiveButtonText: String, 
                          val positiveButtonListener: DialogInterface.OnClickListener,
                          val negativeButtonText: String, 
                          val negativeButtonListener: DialogInterface.OnClickListener,  
                          neutralButtonText: String, 
                          neutralButtonListener: DialogInterface.OnClickListener
                          ) : DialogFragment() {

however I want the last 2 parameters to be optional.

How can I achieve this?

I can't set neutralButtonListener: DialogInterface.OnClickListener = null because DialogInterface.OnClickListener is a non null type.

7 Answers

Default parameters to the rescue.

class AlertDialogFragment(
    context: Context,
    val positiveButtonText: String,
    val positiveButtonListener: DialogInterface.OnClickListener,
    val negativeButtonText: String,
    val negativeButtonListener: DialogInterface.OnClickListener, 
    neutralButtonText: String = "",
    neutralButtonListener: DialogInterface.OnClickListener = OnClickListener {}
) : DialogFragment()

Basically, Kotlin will generate multiple methods, for each combination of parameters possible.

If could also be better to include @JvmOverload annotation on the constructor to allow the same thing in Java.

Check this out

class AlertDialogFragment(  
    context: Context,  
    val positiveButtonText: String,  
    val positiveButtonListener: DialogInterface.OnClickListener,  
    val negativeButtonText: String,  
    val negativeButtonListener: DialogInterface.OnClickListener,  
    neutralButtonText: String = "",  
    neutralButtonListener: DialogInterface.OnClickListener ?= null
) : DialogFragment() { }

You may try this:

class AlertDialogFragment(
    context: Context,  
    val positiveButtonText: String,  
    val positiveButtonListener: DialogInterface.OnClickListener,  
    val negativeButtonText: String,  
    val negativeButtonListener: DialogInterface.OnClickListener,  
    neutralButtonText: String = "",  
    neutralButtonListener: DialogInterface.OnClickListener = OnClickListener {}  
) : DialogFragment() { }

In general you would provide no-op or null-value implementations like

neutralButtonText: String = ""
neutralButtonListener: OnClickListener = OnClickListener {}

But in your use-case you must not use constructor parameters at all!

Fragments can be recreated from the system and require a default constructor.

You need to communicate with the activity or parentFragment by letting these implement the interface you require.

If you want to call kotlin function from Java file which has default arguments

Then use this annotation for your kotlin function

@JvmOverloads

Quite simple...

For class use

class MyClass(param1: Int, param2: Boolean) {
    constructor(param1: Int) : this(param1, true)
    constructor(param2: Boolean) : this(10, param2)
}

and for functions

@JvmOverloads
fun yourMethod(param1: Int, param2: Boolean = true) {
   // ... define your method
}

Now you can call this method any of the below

yourMethod(1)

yourMethod(1, true)

yourMethod(param1 = 1, param2 = false) etc.,

We can add the optional paramater like below. z is optional here

  fun main() {
 add(5,6,7)

}

fun add(x:Int,y:Int,z:Int?=0){
    if(z!=null && z>0)
    println(x+y+z)
    else
    println(x+y)
}
Related