Often, we need to declare a class property, which should depend on the lifecycle state of Activity or Fragment. In this recipe, we are going to employ both the Kotlin Lazy delegate and the Lifecycle class provided by the Android Architecture Components library (https://developer.android.com/topic/libraries/architecture/). We are going to implement a custom property delegate that will provide values in a lazy manner. This means that they are going to be instantiated only on the first call. Moreover, we are going to clear their values once Activity or Fragment gets destroyed. This will avoid memory leaks, which can be caused by managing properties dependent on the Context instance with the standard Lazy delegate.
United States
United Kingdom
India
Germany
France
Canada
Russia
Spain
Brazil
Australia
Argentina
Austria
Belgium
Bulgaria
Chile
Colombia
Cyprus
Czechia
Denmark
Ecuador
Egypt
Estonia
Finland
Greece
Hungary
Indonesia
Ireland
Italy
Japan
Latvia
Lithuania
Luxembourg
Malaysia
Malta
Mexico
Netherlands
New Zealand
Norway
Philippines
Poland
Portugal
Romania
Singapore
Slovakia
Slovenia
South Africa
South Korea
Sweden
Switzerland
Taiwan
Thailand
Turkey
Ukraine