Skip to content

Instantly share code, notes, and snippets.

@gauravrai1
Created June 7, 2020 16:52
Show Gist options
  • Save gauravrai1/5ccc6a263f12a7f3f09786769da125a3 to your computer and use it in GitHub Desktop.
Save gauravrai1/5ccc6a263f12a7f3f09786769da125a3 to your computer and use it in GitHub Desktop.
Example of passing data using Interface in Android (Kotlin)
class FileToGetDataIn extends Activity, cat{
lateinit var mahInterface: cat;
override fun onCreate() {
// Pass as intentt
FileToPassDataFrom().apply(this.setCatInterface(this@FileToPassDataFrom)
}
// Will receive data here
override fun giveName(name: String) {
print("Cat's name is $name")
}
}
class FileToPassDataFrom extends Activity {
// will be inittialised when starting the activity from the source activit/fragment
lateinit var catInterface: cat;
override fun onCreate() {
// invoke interface and pass the data
catInterface.giveName("Champa")
}
// This fucntion will set the interface from the source activit/fragment
fun setCatInterface(catInterface: cat) {
[email protected] = catInterface
}
// Interface declaration
Interface cat{
giveName(name: String)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment