Kelvin Watson
Sep 1, 2023

We recently encountered a similar situation where we needed a lifecycleOwner inside a recycler view viewHolder as well. Because of some indirection we had, we ended up setting the `ViewTreeLifecycleOwner.set(itemView, viewLifecycleOwner)` in onCreateViewHolder, and then in the ViewHolder itself, instead of passing in the viewLifecycleOwner to each instance, we called ```itemView.findViewTreeLifecycleOwner()?.let { lifecycleOwner -> viewModel.liveData.observe(lifecycleOwner) ... ``` only on the views that needed to observe the liveData

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kelvin Watson
Kelvin Watson

Responses (1)

Write a response