Chapter 25 Challenge 1

I was wondering if some one could take a look at this and see if it is a viable solution.

in PhotoGalleryFragment’s onCreateView i made the following changes:

    /** Chapter 25 challenge 1 **/
    viewLifecycleOwnerLiveData.observe(viewLifecycleOwner, Observer {
        it.lifecycle.addObserver(thumbnailDownloader.viewLifecycleObserver)
    })

/** Instead of **/
// viewLifecycleOwner.lifecycle.addObserver(
// thumbnailDownloader.viewLifecycleObserver
// )

Everything seems to be working fine across rotation. However, I wasn’t able to find many examples of the usage. So this was pretty much just me winging it.

1 Like

There is a recommendation in the book:

When a non-null value is published, observe the fragment’s view lifecycle, accessible through
viewLifecycleOwner.lifecycle.

So, in my opinion, there should be a null check.

Yeah, I think so.
It should be like:

viewLifecycleOwnerLiveData.observe(viewLifecycleOwner, {
        it?.lifecycle?.addObserver(
            thumbnailDownloader.viewLifecycleObserver
        )
    })