Reproducible sample with Fix for Memory Leak in RecyclerView Adapter

Last update: Jun 7, 2022

Memory Leak RecyclerView Adapter


Reproducible Sample with Fix


Video Instructions: https://www.youtube.com/c/awesomedevnotes


Android Badge androiddevnotes GitHub badge


Memory Leak RecyclerView Adapter awesomedevnotes - androiddevnotes youtube thumbnail


Code

Only the relevant and important code is shown in README. Refer to entire source code for more details.

A1ListFragment.kt

// Important part is where adapter is set to null in `onDestroyView`

/** Fragment containing RecyclerView */
class A1ListFragment : Fragment(), A1ListRecyclerViewAdapter.OnA1ListItemClickedListener {

    private var _binding: FragmentListA1Binding? = null
    private val binding get() = _binding!!

    // Making RecyclerView adapter nullable so we could set it to null in onDestroyView
    private var a1ListRecyclerViewAdapter: A1ListRecyclerViewAdapter? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentListA1Binding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setup()
    }

    private fun setup() {
        setupRecyclerView()
    }

    private fun setupRecyclerView() {
        a1ListRecyclerViewAdapter = A1ListRecyclerViewAdapter(this)
        binding.a1ListRecyclerView.adapter = a1ListRecyclerViewAdapter
    }

    override fun onDestroyView() {
        // Important to set fields to `null` to prevent memory leaks.
        // We are setting fields to null before `super.onDestroyView()` because official ViewBinding
        // sample, Mozilla Fenix app also do that. I do not know the exact reason.

        a1ListRecyclerViewAdapter = null
        _binding = null
        super.onDestroyView()
    }

    override fun onA1ListItemClicked() {
        findNavController().navigate(R.id.action_a1ListFragment_to_a2DetailFragment)
    }
}

Dependencies

debugImplementation "com.squareup.leakcanary:leakcanary-android:2.7"

// https://github.com/square/leakcanary - To detect memory leaks

💻 Find us on

GitHub

https://github.com/androiddevnotesyoutube/memory-leak-recyclerview-adapter
You might also like...

ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

Aug 9, 2022

A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

Aug 12, 2022

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Aug 9, 2022

RecyclerView : SleepQualityTracker with RecyclerView app

RecyclerView : SleepQualityTracker with RecyclerView app

RecyclerView - SleepQualityTracker with RecyclerView app SleepQualityTracker with RecyclerView This app builds on the SleepQualityTracker developed pr

May 14, 2022

TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.

TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.

TikTok-RecyclerView Demo About This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoP

Jun 16, 2022

Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView Simple and easy way to Paginating a RecyclerView Android

Jan 3, 2022

An adapter to create Android RecyclerViews with sections, providing headers and footers.

An adapter to create Android RecyclerViews with sections, providing headers and footers.

⚠ This library is no longer maintained ⚠️ SectionedRecyclerView An adapter to create Android RecyclerViews with sections, providing headers and footer

Aug 17, 2022

Yet another adapter delegate library.

Yet another adapter delegate library.

Yet another adapter delegate library. repositories { ... maven { url 'https://jitpack.io' } } ... dependencies { implementation("com.git

Jul 30, 2022

Shimo is an adapter for Moshi which randomizes the order of keys when serializing and deserializing

Shimo Shimo is a JsonAdapter.Factory for Moshi which randomizes the order of keys when serializing objects to JSON and when deserializing objects from

Jul 1, 2022
Android library defining adapter classes of RecyclerView to manage multiple view types
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Jul 12, 2022
A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.
A Common RecyclerView.Adapter  implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

##PowerfulRecyclerViewAdapter A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as

Apr 22, 2022
kotlin dsl for kids to simplify RecyclerView.Adapter logic

KidAdapter RecyclerView adapter for kids. A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter. With KidAdapter y

Aug 3, 2022
A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps
A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps

Infinite Recycler View A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps. This library offers you a custom adapt

Dec 10, 2019
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.

xAdapter: Kotlin DSL 风格的 Adapter 封装 1、简介 该项目是 KotlinDSL 风格的 Adapter 框架封装,用来简化 Adapter 调用,思想是采用工厂和构建者方式获取 Adapter 避免代码中定义大量的 Adapter 类。该项目在 BRVAH 的 Ada

Jul 27, 2022
RecyclerView With No Adapter | Available For Jetpack Compose
RecyclerView With No Adapter | Available For Jetpack Compose

About This Project Available on Google Dev Library Click Here RecyclerView No Adapter (Adapter Has Been Handled) RecyclerView No Adapter Using ViewBin

Aug 4, 2022
Easy RecyclerView Adapter

GenericAdapter Easy RecyclerView Adapter Getting started build.gradle allprojects { repositories { // ... maven { url 'https://jit

Dec 3, 2021
Add RecyclerView, use Adapter class and ViewHolder to display data.
Add RecyclerView, use Adapter class and ViewHolder to display data.

فكرة المشروع في هذا المشروع سنقوم بعرض قائمة من البيانات للطلاب على واجهة تطبيق Android بإستخدام: مفهوم RecyclerView مفهوم Adapter مفهوم ViewModel محت

Nov 18, 2021
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

Aug 9, 2022