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
Great Britain
India
Germany
France
Canada
Russia
Spain
Brazil
Australia
Singapore
Hungary
Ukraine
Luxembourg
Estonia
Lithuania
South Korea
Turkey
Switzerland
Colombia
Taiwan
Chile
Norway
Ecuador
Indonesia
New Zealand
Cyprus
Denmark
Finland
Poland
Malta
Czechia
Austria
Sweden
Italy
Egypt
Belgium
Portugal
Slovenia
Ireland
Romania
Greece
Argentina
Netherlands
Bulgaria
Latvia
South Africa
Malaysia
Japan
Slovakia
Philippines
Mexico
Thailand