Created
June 7, 2020 16:52
-
-
Save gauravrai1/5ccc6a263f12a7f3f09786769da125a3 to your computer and use it in GitHub Desktop.
Example of passing data using Interface in Android (Kotlin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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