Chapter 25 Challenge 2

I didn’t understand this challenge completely and probably my solution is not correct but if the fragment will call onStop() just before the onDestroyView, we can remove the viewLifeCyclerObserver and add:

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun clearQueue(){
    Log.i(TAG, "Clearing all request from queue")
    requestHandler.removeMessages(MESSAGE_DOWNLOAD)
    requestMap.clear()
}
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun tearDown(){
        Log.i(TAG, "Destroying background thread")
        fragmentLifeCycle?.removeObserver(this)
        quit()
    }

I also added a property to hold fragment’s lifecycle

  var fragmentLifeCycle:Lifecycle?=null
        set(value){
                field = value
                field?.addObserver(this.fragmentLifecycleObserver)

        }

Finallly in Fragment

   thumbnailDownloader.fragmentLifeCycle = lifecycle

But am I missing?

Thank you in advanced