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