still very new to Kotlin, i'm trying not to call my callback if my activity is null. Let me explain, i have my DataManager that perform async work on some data:
fun performWork(callback: ((param1: T, param2) -> Unit)?) {
// ... async work with data...
// Work is finished, let's call the callback :)
// as the callback is optional:
callback?.invoke(param1, param2)
}
So i call my DataManager into my MainActivity:
DataManager.performWork(callback = { param1, param2 ->
// update the UI now that the work is done.
}
What i want to do is not to call the callback if the callback is null (which means activity is null, or not on screen anymore...)
So in my activity i declare an optional var:
var myCallback: ((params1: T, params2: T) -> Unit)? = null
Then i have a function:
var callbackMain: ((params1: T, params2: T) -> Unit)? = fun (params1: T, params2: T) {
print("ok")
}
Then when i call my DataManager:
mainCallback = this.callbackMain
DataManager.performWork(mainCallback)
mainCallback = null
So the async work is called... and the DataManager enter the callback because mainCallback is not null !!! I think its value is callbackMain. Is there anyway i can make my callback nullable ? So it won't be called, and that my DataManager does not know the activity ?
Thank you very much for any help.