Getting ready
The basic Lazy delegate initialized using the lazy() function provided by the standard library gives the desired possibility of declaring a property of a non-null type, which can only be instantiated after a certain lifecycle event. For example, we reference an element of a screen layout in a property only after the layout was set up inside the Activity.onCreate() hook function.
However, such an implementation using Lazy will cause a memory leak if the property holds a reference to the Activity instance internally, as it will not allow it to be deleted by a garbage collector. The reason for this is that the lazy delegate is caching the instance it's holding. We are going to implement our own property delegate, called LifeCycleAwareLazy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access