Listing 25.19 & 25.20 - 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> (
    private val responseHandler: Handler,
    private val onThumbNailDownloaded: (T, Bitmap) -> Unit
    ) : HandlerThread(TAG) {

    val fragmentLifecycleObserver: DefaultLifecycleObserver =
        object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                Log.i (TAG, "Starting background thread")
                start()
                looper
            }
            override fun onDestroy (owner: LifecycleOwner) {
                Log.i (TAG, "Destroying background thread")
                quit()
            }
        }

    val viewLifecycleObserver: DefaultLifecycleObserver =
        object : DefaultLifecycleObserver {
            override fun onDestroy (owner: LifecycleOwner) {
                Log.i (TAG, "Clearing all requests from queue")
                requestHandler.removeMessages (MESSAGE_DOWNLOAD)
                requestMap.clear()
            }
        }