Reproducible sample with Fix for Memory Leak in RecyclerView Adapter

Overview

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

You might also like...
A flexible view for providing a limited rect window into a large data set,just like a two-dimensional RecyclerView.  It different from RecyclerView is that it's two-dimensional(just like a Panel) and it pin the itemView of first row and first column in their original location. 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

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

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.

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

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

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

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

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

Owner
Awesome Dev Notes | Android Dev Notes YouTube
Awesome Dev Notes | Android Dev Notes YouTube
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

Yoshihito Ikeda 414 Nov 21, 2022
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

null 313 Nov 12, 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

Eugeniu Tufar 56 Nov 27, 2022
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

IB Sikiru 26 Dec 10, 2019
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

ShouHeng 17 Oct 9, 2022
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

Faisal Amir 142 Oct 18, 2022
Easy RecyclerView Adapter

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

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

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

Shaima Alghamdi 3 Nov 18, 2021
Just another one easy-to-use adapter for RecyclerView :rocket:

Elementary RecyclerView Adapter Another one easy-to-use adapter for RecyclerView ?? Features: DSL-like methods for building adapters similar to Jetpac

Roman Andrushchenko 19 Nov 16, 2022
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

XRecyclerView 5.3k Nov 21, 2022