There are several ways to make a property’s lifetime match that of the View in the Fragment. However, you need to be familiar with the Fragment to choose the right method for the requirements you want to meet.
This article organizes the requirements such properties should meet and how to define them and introduces ViewLifecycleProperty that meets all the requirements.
TL;DR
The comparison table of each method is as follows.
ViewLifecycleProperty meets all requirements and is recommended.
Requirements that the property wants to meet
Re-initialization: Can properties be re-initialized according to the View lifecycle?
The lifecycle of Fragment is longer than that of View. I want to be able to re-initialize the properties as the View is regenerated because I want to define it as an immutable property over the View lifecycle rather than an immutable property over the Fragment lifecycle.
Not Null: Can the property be declared Not Null?
Declaring a property as Nullable requires handling Null every time, so I want to declare it as Not Null as much as possible.
Memory saving: Can the property be cleared to null after onDestroyView?
Since onDestroyView does not use the property because its life cycle matches the View. Clearing unused properties with null saves memory (although slightly).
When using Backstack or Navigation, if screen transitions are repeated without clearing null, the Fragment up to that point is still alive and consumes memory for the properties it holds. The memory consumption may be high and the OOM may occur in the dust spider.
onDestroyView: Can access the property in onDestroyView?
Since you may want to do something with onDestroyView such as MediaPlayer, it must be accessible in onDestroyView.
How to define a property
declare with val by lazy
Declare as follows.
val property by lazy { SampleProperty() }
declare a nullable type in var
Declare as follows and assign it in onViewCreated etc.
val property: SampleProperty? = null
declare with lateinit var
Declare as follows and assign it in onViewCreated etc.
lateinit val property: SampleProperty
Use AutoClearedValue
Use AutoClearedValue from android/architecture-components-samples. Declare as follows and assign it in onViewCreated etc.
var property: SampleProperty by autoCleared()
Using ViewLifecycleProperty
Use wada811/ViewLifecycleProperty. Declare as follows.
val property: SampleProperty by viewLifecycle { SampleProperty() }
Alternatively, declare it as follows and assign it in onViewCreated etc.
var property: SampleProperty by viewLifecycle()
Comparison of each method
The comparison of property declaration methods is as follows.
- It cannot be re-initialized by the method declared with val by lazy.
- Not null is not allowed by the method of declaring nullable type in var.
- The method of declaring with lateinit var is not memory saving, but it is easy.
- In the method using AutoClearedValue, IllegalStateException occurs when accessing the property by onDestroyView.
- The ViewLifecycleProperty method meets all requirements. Conclusion: ViewLifecycleProperty meets all requirements and is recommended.
ViewLifecycleProperty
ViewLifecycleProperty is a library that improves on AutoClearedValue and meets all the above requirements.
ViewLifecycleProperty is
- lazy initialization
- accessible when Fragment’s view isn’t null i.e., between onCreateView and onDestroyView
- automatically cleared by null when Fragment’s view released i.e., after onDestroyView
class SampleFragment : Fragment(R.layout.sample_fragment) {
private val property: SampleProperty by viewLifecycle {
SampleProperty(viewLifecycleOwner)
}
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
super.onViewCreated(view, savedInstanceState)
// You can use property
}
override fun onDestroyView() {
super.onDestroyView()
// You can use property unlike AutoClearedValue
}
}
Top comments (0)