Listing 25.9 - Depreciated @OnLifecycleEvent

If you run into problem with @OnLifecycleEvent being depreciated, below seems to be the recommended new way to handle it. Take note of DefaultLifecycleObserver rather than LifecycleObserver.

class ThumbnailDownloader<in T> : HandlerThread(TAG), DefaultLifecycleObserver {

    private var hasQuit = false

    override fun quit(): Boolean {
        hasQuit = true
        return super.quit()
    }

    override fun onCreate(owner: LifecycleOwner) {
        Log.i (TAG, "Starting background thread")
    }

    override fun onDestroy (owner: LifecycleOwner) {
        Log.i (TAG, "Destroying background thread")
    }

    fun queueThumbnail (target: T, url: String) {
        Log.i (TAG, "Got a URL: $url")
    }
}
1 Like