Hi, Would be possible to have a version of this me...
# announcements
j
Hi, Would be possible to have a version of this method using generics? So the interface could be
T
Copy code
fun getListener(): MyListenerInterface {
return when {
            parentFragment is MyListenerInterface -> parentFragment as MyListenerInterface
            targetFragment is MyListenerInterface -> targetFragment as MyListenerInterface
            else -> context as MyListenerInterface
        }
}
k
Yes, with
reified
.
Copy code
interface MyListenerInterface

fun main() {
    val listener = getListener<MyListenerInterface>()
}

inline fun <reified T> getListener(): T {
    return when {
        parentFragment is T -> parentFragment as T
        targetFragment is T -> targetFragment as T
        else -> context as T
    }
}
j
ohh, thanks! I messed it up! I over complicated my code in my attempt to do it.