Skip to content

Instantly share code, notes, and snippets.

View Kuruchy's full-sized avatar
🤘

Bruno Retolaza Kuruchy

🤘
View GitHub Profile
@Kuruchy
Kuruchy / AutoCleaned.kt
Last active April 13, 2022 12:31
Auto Cleaned Value delegate
/**
* Auto Cleaned Value
*
* This solution auto-clears the lazy property by using Kotlin’s delegated properties.
* It will be cleaned when the fragment's view is destroyed, avoiding memory leaks.
*
* Accessing this variable while the fragment's view is destroyed will throw NPE.
*
* @see <a href="https://medium.com/scalereal/let-your-delegates-auto-nullify-references-%EF%B8%8F-3ad6d8875497">Delegates auto nullify</a>
* @see <a href="https://github.com/android/architecture-components-samples/blob/08416a0aced0e4946e0765751a3b20f9a6222708/GithubBrowserSample/app/src/main/java/com/android/example/github/util/AutoClearedValue.kt">AutoClearedValue</a>