Reputation: 176
I have the following interface:
interface ResultReceiver {
fun getSerialNumber(): String
fun getDevicePassword(): String
fun getMacAddress(): String
fun getDevice(): BluetoothDevice
}
Within my DialogFragment I am casting the attached Activity to my interface like so:
resultReceiver = requireActivity() as ResultReceiver
What happens when I perform this type cast?
Here is my activity code to give a little more context to my question:
class ScannerActivity: AppCompatActivity(), ResultReceiver {
...
override fun getSerialNumber(): String {
return serialNumber
}
override fun getDevicePassword(): String {
return devicePassword
}
override fun getMacAddress(): String {
return macAddress
}
override fun getDevice(): BluetoothDevice {
return device
}
}
I am trying to send some Strings to my fragment from my activity.
Upvotes: 1
Views: 585
Reputation: 3255
All the activities are classes which by the end extend from Object
. In Kotlin it's Any
but after compilation it will be Object
again. So you can take any class and try to cast it to your interface. If that class will not have any relation (parent-child) with that interface, it will throw ClassCastException
.
Upvotes: 2