I’m having some trouble understanding the terminology used by the official documentation. Specifically, there are the methods
onViewDetachedFromWindow. What are the difference between these three?
There is a significant difference, even in their signature:
onDetachedFromRecyclerView(RecyclerView recyclerView) – Called by RecyclerView when it stops observing this Adapter.
What you might have not noticed, there is a matching method always called before this one:
onAttachedToRecyclerView(RecyclerView recyclerView) – Called by RecyclerView when it starts observing this Adapter.
When you call
adapter receives call to
onAttachedToRecyclerView(recyclerView). Then following call to
recyclerView.setAdapter() will trigger
You usually don’t need to override this method except for some special circumstances (like keeping count of observed
onViewRecycled(VH holder) is much simpler, it’s called before sending
You can think of it as a “cleanup” method of
onBindViewHolder(VH holder, int position).
onViewDetachedFromWindow(VH holder) always follows a matching
onViewAttachedToWindow(VH holder). It’s called in exact moment when viewholder is becoming visible or invisible (attach/detach calls).
viewHolder was detached but not recycled yet, it’s possible it can receive
onViewAttachedToWindow(ViewHolder) call again without needing to rebind data with