Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write following code in Kotlin for callback implementation


how do i write in Kotlin like java?


Callback callback= new Callback()
 {
     @Override
     public void getCallback(ServerResponse serverResponse) {

     }
 }
like image 644
Kamlakar Kate Avatar asked Jun 29 '17 08:06

Kamlakar Kate


People also ask

How does callback work in Kotlin?

Android has a mechanism called optional callback that allows your methods and classes to receive the failure and success results of another task without having to call another class time and again just to get results. In this tutorial, we will learn about optional callbacks and how to implement them in Kotlin.

What is an object callback Kotlin?

object:Callback() { } is an anonymous class. It has no name when created, before being assigned to var callback . It's similar to the new Callback() code. override replaces @Override. fun indicates that it is a function.


2 Answers

var callback:Callback = object:Callback() {
  override fun getCallback(serverResponse:ServerResponse) {
  }
}

var callback:Callback says that the variable type is a Callback

object:Callback() { } is an anonymous class. It has no name when created, before being assigned to var callback. It's similar to the new Callback() code.

override replaces @Override

fun indicates that it is a function

like image 113
Muz Avatar answered Oct 10 '22 17:10

Muz


You can use following code in Kotlin.

var callback:Callback = object:Callback() {
  fun getCallback(serverResponse:ServerResponse) {
  }
}

You can use this link to convert your Java code to kotlin. https://try.kotlinlang.org

like image 37
Chirag Avatar answered Oct 10 '22 16:10

Chirag