# Avoiding LiveData observer leaks in the Android Fragment lifecycle

DevFeed: [Avoiding LiveData observer leaks in the Android Fragment lifecycle](<https://devfeed.tech/articles/architecture-components-pitfalls-part-1-25878.md>)

Original publisher: [Read original article](<https://bladecoder.medium.com/architecture-components-pitfalls-part-1-9300dd969808?source=rss-54910f05af37------2>)

Author: Christophe Beyls

Published: 2017-10-24T19:31:14Z

Content type: tutorial

Language: en

Sources: [Stories by Christophe Beyls on Medium](<https://devfeed.tech/sources/stories-by-christophe-beyls-on-medium.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [configuration](<https://devfeed.tech/topics/configuration.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [architecture-components](<https://devfeed.tech/tags/architecture-components.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [deprecated](<https://devfeed.tech/tags/deprecated.md>), [fragments](<https://devfeed.tech/tags/fragments.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [livedata](<https://devfeed.tech/tags/livedata.md>), [memory](<https://devfeed.tech/tags/memory.md>), [memory-leak](<https://devfeed.tech/tags/memory-leak.md>), [performance](<https://devfeed.tech/tags/performance.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

This article explains a pitfall when subscribing to LiveData from an Android Fragment. Detaching and re-attaching a Fragment can create a new observer while the previous observer remains active, causing duplicate execution, a memory leak, and a performance problem until the Fragment is destroyed.

## Source excerpt

LiveData and the Fragment lifecycle The new Android Architecure Components are soon to be announced as stable after a few months of public testing. A lot has already been written about the basics (starting with the very good documentation) so I won't cover them here. Instead I would like to focus on important pitfalls that are mostly undocumented and rarely discussed and may cause issues in your applications if you miss them. In this first article, I'll talk about our beloved Fragments. Edit (14 may 2018): Google has finally fixed the issue in support library 28.0.0 and AndroidX 1.0.0. See solution 4 below.Edit (13 march 2020): onActivityCreated() has been officially deprecated and onViewCreated() should be used instead. The code samples in this article have been updated accordingly. The Architecture Components provide default ViewModelProvider implementations for activities and fragments. They allow you to store LiveData instances inside a ViewModel to be reused across configuration changes. The usage with activities is quite straightforward because the activity lifecyle maps well to the Lifecycle interface of the Architecture Components, but the fragment lifecycle is more complex and may cause subtle side effects if you're not being careful. The Fragment lifecycle (simplified version) Fragments can be detached and re-attached. When they are detached, their view hierarchy is destroyed and they become invisible and inactive, but their instance is not destroyed. When they are later re-attached, a new view hierarchy is created and onCreateView() and onViewCreated() are called again. For this reason, the usually recommended place to initialize Loaders and other asynchronous loading operations that will eventually interact with the view hierarchy is in onViewCreated(). We can assume this is also the best place to initialize LiveData instances by subscribing a new Observer. Most of the official Architecture Components samples also do it there. You would expect typical co