The problem is the WeakReference and the local variable that you pass as a listener .
WeakReference known that this object does not contain garbage collection , therefore, if there is no other available reference to it, it can be processed at any time as soon as the method refers to it through a local variable. And this is exactly what happens in your case, as a weak link becomes null .
The solution is to keep a strong reference to an object that is passed as a listener somewhere in the calling code (since it uses an action, the activity itself can store it in the property, so that the listener lifetime will correspond to the activity).
For example, declare a property
lateinit var currentListener: OnBitmapProcessedListener
in the action code, then save the listener created in this property:
val task = PhotoRotationTask(uri, filePath, resolver) task.setOnBitmapProcessedListener(object : OnBitmapProcessedListener {
If several tasks and listeners are possible, take care to keep all listeners.
source share